Building a View

You now have a controller that renders text, but this design can take you only so far. Ideally, you’d like to separate the HTML in the view from your business logic in your controllers and models. The sloppy design is easy enough to fix. If you want to follow Rails MVC conventions, you should render text in a separate view instead of from your controller. Instead of the render command in the greeting controller, you can render the text in a Rails view. As with many web frameworks, Rails can use a template strategy for the view. A template is simply an HTML page with Ruby code mixed in. A Ruby engine called ERb interprets the template on the server, adding dynamic content to your page. That page will usually be written in HTML, but you can design templates for XML pages or email, too.

Rails documentation for the controller

Figure 1-2. Rails documentation for the controller

With Rails, you can generate an empty view and some helpers that the view will need along with your controller. Type the script/destroy controller greeting command to destroy the previous controller:

    $ ruby script/destroy controller greeting

          rm  app/helpers/greeting_helper.rb
          rm  test/functional/greeting_controller_test.rb
          rm  app/controllers/greeting_controller.rb
       rmdir  test/functional
    notempty  test
       rmdir  app/views/greeting
    notempty  app/views
    notempty  app
    notempty  app/helpers
    notempty  app
    notempty  app/controllers
    notempty  app

Next, type script/generate controller greeting index to generate a controller with the index action and index view:

    $ ruby script/generate controller greeting index

      exists  app/controllers/
      exists  app/helpers/
      create  app/views/greeting
      create  test/functional/
      create  app/controllers/greeting_controller.rb
      create  test/functional/greeting_controller_test.rb
      create  app/helpers/greeting_helper.rb
      create  app/views/greeting/index.html.erb

See also Figure 1-3.

Rails documentation for the controller

Figure 1-3. Rails documentation for the controller

The generator created the view, index.html.erb, with helper and test files. From the output, you can see that the generator created a few directories, a controller called greeting_controller.rb, a helper called greeting_helper.rb, and a test called greeting_controller_test.rb. Take a look at the new index method in controller, called app/controllers/greeting_controller.rb:

class GreetingController < ApplicationController

   def index
   end
end

This controller example uses Action Pack, the Rails framework responsible for implementing the view and controller parts of Rails. Unlike most MVC frameworks, your index method didn’t specify a view. If your controller doesn’t explicitly call render, Rails uses naming conventions to decide which view to render. The controller’s name determines the view’s directory, and the action name determines the name of the view. An action is a method on a controller. In this case, Action Pack fires the view in app/views/greeting/index.html.erb. You didn’t have to edit any XML files or type any additional code to wire the controller to the view. You just allowed Rails to provide consistent naming conventions and infer your intent from there.

Now, edit the view. You’ll find this data:

<h1>Greeting#index</h1>
<p>Find me in app/views/greeting/index.html.erb</p>

Point your browser to http://localhost:3000/greetings/index to see the previous message in HTML. Rails tells you where to find the file, should you ever render an unimplemented view. This empty file awaits an implementation.

Get Rails: Up and Running, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.