Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[GUIDE] Get started with Ruby web dev right now

Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
At the end of this guide you will, in 15 to 60 minutes, have a Ruby project which runs for you both locally and is up and running on the web. This is the result:


So today I'm here to give you who are new to programming an easy alternative to PHP with what to get started with web development. The environment is simple to set up and you'll get to try Ruby in a very similar fashion to how you'd typically write your very first PHP pages: you can write HTML with Ruby commands embedded into it where you like.

I thought you'd probably like to get your page online with minimal effort as well, so as a bonus there's a little bit of extra to bare ruby essentials in order to get our web application deployed to Heroku's free hosting. You won't be tied to using Heroku, but at least you'll have one place to start with.

First let's look at what the code will look like.
Code:
<html>
  <body>
  	<h1>Hello, <%= params[:name] || "Whateveryournameis" %></h1>
	 <ul>
	 <% @awesome_things.each do |thing| %>
	   <p><%="#{thing} is full of rage!!!"%></p>
	 <% end %>
	 </ul>
	 <p>By the way, the query parameters are: <%= params %></p>
  <body>
</html>
It looks almost like good old html, doesn't it? Just a few extra ruby expressions embedded there. The file is in .erb (embedded ruby) format. To build your own simple page this file is all you need to edit. There's another important file, though, if you're to support more than one static page. It defines which html (erb) file is returned for each url and each method (GET/POST). Don't be alarmed, nothing too fancy going on here...
Code:
require 'sinatra'
require 'erb'

get '/' do
  @awesome_things = ['RaGEZONE', 'Negata', 'Ruby']
  erb :index
end
I'm sure some of you already guessed how to operate that file. The rest can feel free to ask questions or ask google. The keywords are ruby, sinatra and erb. But anyways, let's go into the tedious part, how to set your system up. This guide is for Windows (7), although normally I would use Linux for any programming. But hey I figured many of you use Windows primarily so there you go. (If you do use Linux, the setup is more or less the same, perhaps a bit shorter even.)

This guide is based on , however it didn't work right off the bat so you may follow my guide and only refer to that one if you need more details or some explanation. (I might have forgotten to mention something, of course!)

Summary
  • Download Heroku Toolbelt, comes with everything you need to download
  • Create project files
  • Run your application locally
  • Push your files to Heroku's hosting

Download and install
1. First download the Heroku tools from
Make a full install, install to C:\Heroku (or any other public path but not under program files, I had some permission problem with that at first although it would probably have been easy to fix). The correct ruby version supported by Heroku (1.9.2) is included with it.

2. Open command line from start -> run -> cmd

I expected the setup to set ruby and git paths automatically but at least for me it didn't, so let's check that first. In the console try "irb". It's a ruby interactive shell where you may write ruby commands, but we won't be using it this time. If it starts then you may skip the next step, but if it doesn't let's set the paths.

2.1 Go to start -> computer (right click) -> properties -> advanced system settings -> environment variables -> Path

2.2 Add to the end of the path line the following: ;C:\Heroku\ruby-1.9.2\bin;C:\Program Files (x86)\Git\bin

Close and reopen the command prompt for the new paths to take effect.

Create project files
3. Open your favorite text editor and create these files into the project's (currently empty) directory.

* views/index.erb - It's the first code block above (note, erb files should be in the "views" directory)
* web.rb - It's the second code block
* Gemfile:
Code:
source 'https://rubygems.org'
gem 'sinatra', '1.4.2'
* Procfile (note the capital P, it's important for Heroku as they run a linux system)
Code:
web: bundle exec ruby web.rb -p $PORT

Test launch your application locally
4. In the console cd into the directory where you created the files, then type
"bundle install"

This looks into the Gemfile and automatically downloads required libraries for you). I don't remember if bundler was installed with Heroku Toolbelt, so if the above fails then install bundler by yourself by typing "gem install bundler" and try again. This will download the program for you, no setup is needed.

5. If everything is perfect you're ready to start your application, but you may need to reinstall foreman for that (I did). Try
"foreman start".

Foreman checks from the Procfile which command to run to launch your application and watches for changes, so if you update your files foreman restarts the app for you automatically. Nice!

5.1 Anyhow if the start fails with something about "bad file descriptor", we'll just switch foreman's version to something that works:
"gem uninstall foreman"
"gem install foreman -v 0.61"

Then do the foreman start again and it should be running properly. You may now view the page at

Push your files to Heroku's hosting
6. For this you'll need a Heroku account, so go ahead and sign up at

7.Type
"heroku login"

Enter your username and password that you signed up to Heroku with. If it asks to create an ssh key answer Y (for me it didn't but I had some old keys lying around which weren't recognized with Heroku so it might not be a problem for you), if it doesn't then trigger the key creation yourself by running "heroku keys:add". You may just hit enter through the few questions and it'll be ok, but know that it would be wise to protect your ssh key with a passphrase. It's your identity on the internet, after all.

7. Now it's time to create the heroku project which has its counterpart on their servers. The Toolbelt makes this easy so simply type
"heroku create" and the rest is magic.

8. Heroku uses git for version control. git is a whole big topic itself so let's not go into that in this post, but it's enough for you to know that it's great and the essentials are this simple:

git init (tells the git program that this directory should be a git project)
git add . (adds every file in this directory to be "committed")
git commit -m "init" (saves the files which you marked with add and writes a log message "init" describing the changes you have made to the files)

At this point git tells me to config my email and name and gives examples, so do that -- it's not related to setting up the environment but git wants to add this information to every commit (nice in log to know who did what)

9. Finally, send the files to Heroku's servers. Heroku does its magic and sets up their environment automatically and launches your app when done.
git push heroku master

It will probably ask if you want to trust this connection, just type yes. Among other info it should also print the url from which you may open your page on their servers, but to make this really easy just type "heroku open" and it will open the page to your browser.

Excellent!

Let me know if you encounter any problems I didn't mention about and I'll update the guide accordingly.
 
Last edited:
Back
Top