Ruby on Rails 3 Hello World Example Tutorial Part 1

In this blog post, I'm going to share my knowledge on writing by first program on Rails3, the classical Hello World and run this till the completion point..i.e. Till you get Hello World from the Rails 3 web application on your browser. As I'm a beginner myself to Ruby, I'm going to keep this very basic and provide step by step instructions wherever applicable. You may wish to compare the version of Ruby and Rails that I'm using in this tutorial with what you have installed, to make sure that you are using the right version.
Ruby Version:ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
Rails Version: Rails 3.0.7
1) Go to the folder where you want to create your first Ruby on Rails Hello World application and issue the following command
C:\myruby>rails new helloworld
      create
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  log
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  public/images
      create  public/images/rails.png
      create  public/stylesheets
      create  public/stylesheets/.gitkeep
      create  public/javascripts
      create  public/javascripts/application.js
      create  public/javascripts/controls.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/effects.js
      create  public/javascripts/prototype.js
      create  public/javascripts/rails.js
      create  script
      create  script/rails
      create  test
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  test/unit
      create  tmp
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
Now, the rails command has taken the name of your new project, which is"helloworld". At the end of this step, it creates a skeleton of your project inside a directory called helloworld. A screenshot of the directory structure created at the end of this step is provided below;
helloworld\.gitignore
helloworld\app
helloworld\config
helloworld\config.ru
helloworld\db
helloworld\doc
helloworld\Gemfile
helloworld\lib
helloworld\log
helloworld\public
helloworld\Rakefile
helloworld\README
helloworld\script
helloworld\test
helloworld\tmp
helloworld\vendor
helloworld\app\controllers
helloworld\app\helpers
helloworld\app\mailers
helloworld\app\models
helloworld\app\views
helloworld\app\controllers\application_controller.rb
helloworld\app\helpers\application_helper.rb
helloworld\app\views\layouts
helloworld\app\views\layouts\application.html.erb
helloworld\config\application.rb
helloworld\config\boot.rb
helloworld\config\database.yml
helloworld\config\environment.rb
helloworld\config\environments
helloworld\config\initializers
helloworld\config\locales
helloworld\config\routes.rb
helloworld\config\environments\development.rb
helloworld\config\environments\production.rb
helloworld\config\environments\test.rb
helloworld\config\initializers\backtrace_silencers.rb
helloworld\config\initializers\inflections.rb
helloworld\config\initializers\mime_types.rb
helloworld\config\initializers\secret_token.rb
helloworld\config\initializers\session_store.rb
helloworld\config\locales\en.yml
helloworld\db\seeds.rb
helloworld\doc\README_FOR_APP
helloworld\lib\tasks
helloworld\lib\tasks\.gitkeep
helloworld\log\development.log
helloworld\log\production.log
helloworld\log\server.log
helloworld\log\test.log
helloworld\public\404.html
helloworld\public\422.html
helloworld\public\500.html
helloworld\public\favicon.ico
helloworld\public\images
helloworld\public\index.html
helloworld\public\javascripts
helloworld\public\robots.txt
helloworld\public\stylesheets
helloworld\public\images\rails.png
helloworld\public\javascripts\application.js
helloworld\public\javascripts\controls.js
helloworld\public\javascripts\dragdrop.js
helloworld\public\javascripts\effects.js
helloworld\public\javascripts\prototype.js
helloworld\public\javascripts\rails.js
helloworld\public\stylesheets\.gitkeep
helloworld\script\rails
helloworld\test\fixtures
helloworld\test\functional
helloworld\test\integration
helloworld\test\performance
helloworld\test\test_helper.rb
helloworld\test\unit
helloworld\test\performance\browsing_test.rb
helloworld\tmp\cache
helloworld\tmp\pids
helloworld\tmp\sessions
helloworld\tmp\sockets
helloworld\vendor\plugins
helloworld\vendor\plugins\.gitkeep
2) Now, you have to generate a controller which will handle your hello world request. This controller will "respond" to our request for helloworld, when initiated through the browser. To generate the controller, execute the following command;
C:\myruby\helloworld>rails generate controller welcome
      create  app/controllers/welcome_controller.rb
      invoke  erb
      create    app/views/welcome
      invoke  test_unit
      create    test/functional/welcome_controller_test.rb
      invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke    test_unit
      create      test/unit/helpers/welcome_helper_test.rb
Now, the controller file is named as welcome_controller.rb after the name we provided to the controller. Now, if you open this controller file, you will find the following piece of code;
class WelcomeController < ApplicationController
end
Now, you have to make this controller to respond to a request of "hello". To do this, edit the welcome.controller.rb file to define an action. Do the following as shown below;
class WelcomeController < ApplicationController
def hello
 @message = 'Hello World! My first Ruby on Rails 3 program'
end
end
We have defined a method called "hello" and set a variable "message" to what we want to show on the browser. We are not done yet. We need to create a HTML template which will be used to show this information on the screen. Refer to Part 2 of this tutorial to understand more.

No comments:

Post a Comment