February 17, 2011 | In: Hello World, Ruby on Rails
Getting to ‘Hello World’: Ruby on Rails on Windows
I’ve started playing with Rails. So far it looks really great. I get it. I do. The concept of Convention over Configuration – it makes a ton of sense. Here’s where you lose me Rails: how can I install/set up/get to ‘Hello World’?
I want to code. I don’t want to configure right? Ok, let’s say you’re new to .Net. What do you do?
- Download Visual Studio Express.
- File->New Project
- Write some code – Response.Write(“Hello World”);
- Hit F5
- Rejoice!
5 steps. One of those steps involves hitting a single button and another involves rejoicing.
Now let’s say you’re like me and you want to write a small Hello World Ruby on Rails app using the latest version of Ruby and the latest version of Rails.
Here is what you’ll need to do. Bit tricky. Doable yes, but someone (not it!) needs to write a nice wrapped up Windows MSI for this:
- Install Ruby from http://rubyforge.org/frs/?group_id=167 (version 1.92-p136 as of this writing). Version is crazy important in rails. Make sure you tick the box that adds Ruby to your PATH.
- Download and install Ruby gems, it’s like apt-get (or er, windows update?). It’s a package manager that lets you download stuff and keep it up to date.
- Get it here: http://rubyforge.org/frs/?group_id=126 (version 1.5.2 as of this writing)
- Extract it
- Figure out where you extracted it. Open a cmd prompt by going to Start->Run and typing ‘cmd’ and navigate to that directory by typing cd c:\downloads\ruby-gems or whatever your folder is
- once you are properly in the folder run ruby setup.rb
- Now gem is installed so we can install rails. At the cmd prompt run gem install rails. Pretty easy huh?
- Install the Sqlite gem (required to run your first rails ‘hello world’ app, even if you don’t want to use a database at all). For some reason there’s an issue with this on Windows. But it’s pretty easy to get going:
- At the cmd prompt run gem install sqlite3
- After the command completes, grab this zip: http://www.sqlite.org/sqlitedll-3_7_3.zip
- Get the extracted files an place them in your /ruby/bin/ directory where you installed ruby in step 1.
- Now we’re ready to create our first app. At your trusty cmd prompt run rails new /path/to/your/app
- Start your rails server by running cd /path/to/your/app followed by rails server. Now you can browse to http://localhost:3000 to see the default rails application
- Now Ruby on Rails is fully installed! Rejoi-er-what about ‘Hello World’? Ah yes, we still have not written a single line of code. Well, here’s where Rails shines a bit. Go back to your trusty cmd prompt and run rails generate controller Greetings hello This will actually generate a controller called ‘Greetings’ with an action/view called ‘hello’.
- For this simple example go to /path/to/your/app in Windows Explorer. Find the /app/controllers sub-directory. In there you should see your Controller – greetings_controller.rb. Edit the file in your favorite Ruby on Rails editor to look like this:
class GreetingsController < ApplicationController def hello @message = "Hellow world!" end end
- Now, let's edit our view. Back to the /app/views/greetings/ folder we'll see hello.html.erb. We can edit that to resemble:
<h1>Greetings#hello</h1> <p><%= @message %></p>
- Now, at last we can navigate to http://localhost:3000/greetings/hello
So now that I've been up and running with Rails almost a week I still think the setup was far and away the most challenging/least documented part. Which is what this post will hopefully solve for someone else down the line. Unfortunately, Rails changes so quickly that this will likely be out of date quite soon.
Ah, yes and lest we forget:
11. Rejoice
4 Responses to Getting to ‘Hello World’: Ruby on Rails on Windows
Luis Lavena
February 18th, 2011 at 2:09 pm
Hello,
I think you missed the point on comparing Hello World on a *language* versus usage of a Framework
If you tried to do the same with dotNET and aspNET you would still require to create a solution, configure your IIS to serve the request, write some XML, etc… until you rejoice.
Ruby is the language, and a hello world is simple:
puts “Hello World”
Saved into a file called hello.rb and invoked from the command line.
You could have obtained all this also from an IDE, like RubyMine or NetBeans.
Or if you want to compare your Visual Studio example, take Sinatra:
require ’sinatra’
get ‘/’ do
“Hello World”
end
Save to app.rb and run: ruby app.rb
(having Sinatra gem installed)
Beyond all that, all your steps could have been simplified using RailsInstaller: http://railsinstaller.org/
Brendan
February 18th, 2011 at 6:43 pm
I hear you Luis – .Net or rather Visual Studio ships with a light local dev web server (similar to Webrick?) so it’s rather trivial to even build a Hello World Asp.Net MVC app.
But I get your point. The problem I had with Rails Installer is that I wanted Ruby 1.92. As I play more and more with Rails (and I am admittedly very new to it) I do like it. What I miss from the .Net side is a single, authoritative source for IDE, setup etc.
Obviously that approach has it’s pitfalls as well.
Luciano
December 24th, 2011 at 10:36 pm
The rails hello world example has a problem with yaml with ruby 1.9.3.
It’s necessary to fix
..ruby-1.9.3\lib\ruby\gems\1.9.1\gems\activesupport-2.3.4\lib\active_support\locale\en.yml
in order to avoid:
couldn’t parse YAML at line
see:
http://stackoverflow.com/questions/4980877/rails-error-couldnt-parse-yaml
answer 24
Srimanta Chakraborty
January 20th, 2012 at 8:07 am
Thanks a lot for the above mentioned step by step procedures to install ruby on rails. Now I am starting to play with Ruby.
Thanks again…