Executing your first app in Ruby on Rails

0
1064

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. The following Dialog-Box will appear.
  8. 3

  9. After you create your first application switch to its folder by giving command cd FirstApp
  10. To see your FirstApp on the web server , Start the web server ,by giving command rails server OR rails s

  11. 4

  12. After that press enter you will see the following dialog box.
  13. 5

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

  16. 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

  17. To get rails saying “Hello Rails!” you have to create a controller and view.
  18. A controller’s purpose is to receive specific requests for the application where as routing decides which controller receives which requests.
  19. A view’s purpose is to display this information in a human readable format.
  20. 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
  21. Enter the following command: rails generate controller home index
  22. Rails will create several file and route you
  23. 7

  24. 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
  25. Open the app/views/home/index.html.erb file in your text editor
  26. Delete all of the existing code in the file, and replace it with the following single line of code
  27. 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.
  28. You have to tell Rails where your actual home page is located that is FirstAppconfigroutes.rb
  29. 8

  30. 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.
  31. Find the line beginning with root and uncomment it. It should look something like the following :

    # root ‘welcome#index’
  32. 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’
  33. Now replace the statement to map your home page with the following

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

  36. 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.
  37. This was created earlier when you started the controller generator (rails generate controller home index).
  38. If you know navigate to the http://localhost:3000/ you will see the following window.
  39. 10

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here