4.2 Say "Hello", Rails
To get Rails saying "Hello", you need to create at minimum a controller and a view.A controller's purpose is to receive specific requests for the application. Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions. Each action's purpose is to collect information to provide it to a view.
A view's purpose is to display this information in a human readable format. An important distinction to make is that it is the controller, not the view, where information is collected. The view should just display that information. By default, view templates are written in a language called eRuby (Embedded Ruby) which is processed by the request cycle in Rails before being sent to the user.
To create a new controller, you will need to run the "controller" generator and tell it you want a controller called "welcome" with an action called "index", just like this:
$ bin/rails generate controller welcome index |
create app/controllers/welcome_controller.rb route get 'welcome/index'invoke erbcreate app/views/welcomecreate app/views/welcome/index.html.erbinvoke test_unitcreate test/controllers/welcome_controller_test.rbinvoke helpercreate app/helpers/welcome_helper.rbinvoke test_unitcreate test/helpers/welcome_helper_test.rbinvoke assetsinvoke coffeecreate app/assets/javascripts/welcome.js.coffeeinvoke scsscreate app/assets/stylesheets/welcome.css.scss |
app/controllers/welcome_controller.rb
and the view, located at app/views/welcome/index.html.erb.Open the
app/views/welcome/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:<h1>Hello, Rails!</h1>