Web Programming TutorialsExecuting your first app in Ruby on Rails

Executing your first app in Ruby on Rails

In this tutorial today we will learn to create and run our first application in Ruby on Rails.

  • To create and run the application follow the steps given below :

  1. Go to start and open the “Command Prompt with Ruby and rails”
  2. Start->All Programs-> RailsInstaller-> Command Prompt with Ruby and Rails as shown in the image
  3. 1

  4. The Following Dialog Box Will Appear
  5. 2

  6. To create new Project,type the command as rails new FirstApp
  7. rails new FirstApp

  8. The following Dialog-Box will appear.
  9. 3

  10. After you create your first application switch to its folder by giving command cd FirstApp
  11. cd FirstApp.

  12. To see your FirstApp on the web server , Start the web server ,by giving command rails server OR rails s
  13. rails  server or rails s


  14. After that press enter you will see the following dialog box.
  15. 5

  16. To see your application running you have to look it in the web browser by providing url http://localhost:3000/
  17. 6

  18. To Display Hello, Rails! take up with some of the previous steps quickly.
    1. Repeat from step 3 to step 9 as mentioned above.
    2. You have to ensure that your server is still running.
    3. You can do this by running the following in you root directory
    4. rails server 

  19. To get rails saying “Hello Rails!” you have to create a controller and view.
  20. A controller’s purpose is to receive specific requests for the application where as routing decides which controller receives which requests.
  21. A view’s purpose is to display this information in a human readable format.
  22. To create a new controller, you will need to run the “controller” generator and tell it that you want a controller called “home” with an action called “index
  23. Enter the following command: rails generate controller home index
  24. rails generate controller home index

  25. Rails will create several file and route you
  26. 7

  27. Most important thing here is that the controller is located at app/controllers/home_controller.rb and the view is located at app/views/home/index.html.erb
  28. Open the app/views/home/index.html.erb file in your text editor
  29. Delete all of the existing code in the file, and replace it with the following single line of code
  30. <h1>Hello, Rails! </h1>

  31. For setting the Application Home Page,we need to tell Rails when we want Hello Rails! to be displayed. In our case, we want it to display when we navigate to the root URL of our site, http://localhost:3000.
  32. You have to tell Rails where your actual home page is located that is FirstAppconfigroutes.rb
  33. 8

  34. This is your application’s routing file which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action.
  35. Find the line beginning with root and uncomment it. It should look something like the following :

    # root ‘welcome#index’
  36. In this you can see the statement followed by the # sign this means you have commented the line so now you have to uncomment it just by removing # from the statement which will look like as shown below:

    root ‘welcome#index’
  37. Now replace the statement to map your home page with the following

    root ‘home#index’
  38. This can be viewed as shown below :
  39. 9

  40. The root “welcome#index” tells Rails to map requests to the root of the application to the home controller’s index action and get “home/index” tells Rails to map requests to http://localhost:3000/home/index to the home controller’s index action.
  41. This was created earlier when you started the controller generator (rails generate controller home index).
  42. If you know navigate to the http://localhost:3000/ you will see the following window.
  43. 10

  44. Thus we have successfully created our first app in Ruby on Rails.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -