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 :
- Go to start and open the “Command Prompt with Ruby and rails”
- Start->All Programs-> RailsInstaller-> Command Prompt with Ruby and Rails as shown in the image
- The Following Dialog Box Will Appear
- To create new Project,type the command as rails new FirstApp
- The following Dialog-Box will appear.
- After you create your first application switch to its folder by giving command cd FirstApp
- To see your FirstApp on the web server , Start the web server ,by giving command rails server OR rails s
- After that press enter you will see the following dialog box.
- To see your application running you have to look it in the web browser by providing url http://localhost:3000/
- To Display Hello, Rails! take up with some of the previous steps quickly.
- Repeat from step 3 to step 9 as mentioned above.
- You have to ensure that your server is still running.
- You can do this by running the following in you root directory
- To get rails saying “Hello Rails!” you have to create a controller and view.
- A controller’s purpose is to receive specific requests for the application where as routing decides which controller receives which requests.
- A view’s purpose is to display this information in a human readable format.
- 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“
- Enter the following command: rails generate controller home index
- Rails will create several file and route you
- 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
- Open the app/views/home/index.html.erb file in your text editor
- Delete all of the existing code in the file, and replace it with the following single line of code
- 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.
- You have to tell Rails where your actual home page is located that is FirstAppconfigroutes.rb
- 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.
- Find the line beginning with root and uncomment it. It should look something like the following :
# root ‘welcome#index’
- 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:
- Now replace the statement to map your home page with the following
- This can be viewed as shown below :
- 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.
- This was created earlier when you started the controller generator (rails generate controller home index).
- If you know navigate to the http://localhost:3000/ you will see the following window.
- Thus we have successfully created our first app in Ruby on Rails.
rails new FirstApp
rails server or rails s
rails generate controller home index
<h1>Hello, Rails! </h1>