Ruby on Rails 3 Hello World Tutorial Example Part 2

We are trying to create our first Hello World program for Ruby on Rails, and this is part 2 of this tutorial. In Part1 of this Rails Hello World Example,we created a project and then defined our controller. We also added some code into the controller to respond to a request of "hello" from the web browser. For this, we defined a method inside the controller and set a variable in this method, the value of which we want to dump on the browser. Now, if you inspect the "app/views" folder inside your project directory, you will find a folder called "welcome" (mapping to our controller). Now, this folder is linked to our controller "welcome" created in part 1. We will now add contents into this folder, which will be used to respond with proper HTML to the actions we created in the control files in part1.

1)In the controller file, we had an action named "hello". So, inside the app/views/welcome folder, we will create a file called hello.html.erb which will be used as a template to respond to the action of "hello". Rails automatically renders the file, when it is named in this fashion. ERb is Embedded Ruby output tags. Create a file with the name hello.html.erb and put the contents as shown below;
<html>
<body>
Welcome Message!
<h3><%= @message %></h3>
</body>
</html>
You should now be able to make out what is happening here.The value of the variable message used in the controller.rb file is being used when rendering the HTML file for us.

Ok, so we have linked the controller to viewer successfully. Now, when you make a request for hello through a URL, how do we tell our Rails application to respond to that request? We do that by editing the config/routes.rb file inside the project folder.

2) Edit the config/routes.rb file and add the information as shown below;
Helloworld::Application.routes.draw do
 match ':controller(/:action(/:id(.:format)))'  
end

3) All set now. You can start the built in web server by using "rails server" command inside the project folder.
C:\myruby\helloworld>rails server
=> Booting WEBrick
=> Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-06-04 18:21:06] INFO  WEBrick 1.3.1
[2011-06-04 18:21:06] INFO  ruby 1.9.2 (2011-02-18) [i386-mingw32]
[2011-06-04 18:21:06] INFO  WEBrick::HTTPServer#start: pid=5856 port=3000

4) If you access "http://localhost:3000/welcome/hello", you should see your first Hello World program in Rails on action..A screenshot below shows the output from the browser;
Rails - Hello World Example Output
Rails - Hello World Example Output
If you look at the output in the web server logs, you will find what happened behind the scenes as you executed the request on the browser;
Started GET "/welcome/hello" for 127.0.0.1 at 2011-06-04 18:22:52 +1000
  Processing by WelcomeController#hello as HTML
Rendered welcome/hello.html.erb within layouts/application (3.0ms)
Completed 200 OK in 33ms (Views: 33.0ms | ActiveRecord: 0.0ms)
And Congrats, you have completed your Hello World program in Ruby on Rails. If you are getting any errors at any stage, let us know..We will have a look.

5 comments:

  1. Tried a few helloworlds with no luck but got yours going.

    Now how to move a 4 page static site site with buttons would be a great start to then progress with some dbase and coding wizardry.

    ReplyDelete
  2. @Anonymous, sure.stay connected to us. Some more tutorials on Rails are on the way.

    ReplyDelete
  3. Excellent! Do you offer any professional training.
    With warm regards,
    Raja

    ReplyDelete
  4. AFTER trying you tutorial i got this error at the end

    [2012-06-29 10:40:43] INFO WEBrick 1.3.1
    [2012-06-29 10:40:43] INFO ruby 1.9.3 (2012-04-20) [i686-linux]
    [2012-06-29 10:40:43] WARN TCPServer Error: Address already in use - bind(2)
    Exiting
    /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `new'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `block in create_listeners'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:82:in `each'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:82:in `create_listeners'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:82:in `listen'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:70:in `initialize'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:45:in `initialize'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `new'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `run'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/server.rb:265:in `start'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands/server.rb:70:in `start'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands.rb:55:in `block in '
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands.rb:50:in `tap'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands.rb:50:in `'
    from script/rails:6:in `require'
    from script/rails:6:in `'
    niraj@niraj-Dell-System-Inspiron-N4110:~/Documents/rails/hello$ rails server
    => Booting WEBrick
    => Rails 3.2.6 application starting in development on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server
    [2012-06-29 10:41:16] INFO WEBrick 1.3.1
    [2012-06-29 10:41:16] INFO ruby 1.9.3 (2012-04-20) [i686-linux]
    [2012-06-29 10:41:16] WARN TCPServer Error: Address already in use - bind(2)
    Exiting
    /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `new'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `block in create_listeners'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:82:in `each'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:82:in `create_listeners'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:82:in `listen'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:70:in `initialize'
    from /home/niraj/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:45:in `initialize'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `new'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `run'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/server.rb:265:in `start'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands/server.rb:70:in `start'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands.rb:55:in `block in '
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands.rb:50:in `tap'
    from /home/niraj/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands.rb:50:in `'
    from script/rails:6:in `require'
    from script/rails:6:in `'
    ...
    help me

    ReplyDelete
  5. @Phpman,In the command prompt type this "netstat -n -a -o". there u can see port 3000 will be used by some other application. so take that pid,go task manager->process->show process from all user. then search for that pid, and end the process. then your application runs

    ReplyDelete