Saturday, December 3, 2011

Hello world in Ruby on Rails step by step


I just started to learn ROR just because it is quite similar to Django


So, this is a step by step tutorial to getting over the first hurdle: Creating a “hello world” application in Ruby on Rails. This should work assuming you already have Rails 3 installed and running on your system:
  1. $ rails hello
  2. $ cd hello
  3. $ rails generate controller hello
  4. Open the file config/routes.rb. Almost at the bottom (line #57) is this line:
    # match ':controller(/:action(/:id(.:format)))'
    Remove the # in front so the line looks like:

    match ':controller(/:action(/:id(.:format)))'
  5. Create a file named index.html.erb in app/views/hello containing the text ‘Hello world’.
  6. $ rails server
  7. Navigate to http://localhost:3000/hello in your browser and be greeted by your friendly application: “Hello world”.
PS: http://localhost:3000/ is (still) a lot prettier “Hello world”-ish page.
PPS: Note that the Rails 3 example above actually has a step more than the Rails 1 ditto. This comes from the fact that Rails 3 doesn’t build a default route for you.
The code in this article has been verified with:
  • Ruby: 1.8.7
  • Rails: 3.0.0.beta

No comments:

Post a Comment