1 this file is 2 class

This preview shows page 147 - 149 out of 517 pages.

1 # This file is app/controllers/movies_controller.rb 2 class MoviesController < ApplicationController 3 def index 4 @movies = Movie.all 5 end 6 end 1 -# This file is app/views/movies/index.html.haml 2 %h1 All Movies 3 4 %table#movies 5 %thead 6 %tr 7 %th Movie Title 8 %th Rating 9 %th Release Date 10 %th More Info 11 %tbody 12 - @movies.each do |movie| 13 %tr 14 %td= movie.title 15 %td= movie.rating 16 %td= movie.release_date 17 %td= link_to "More about #{movie.title}", movie_path(movie)
Image of page 147
Figure 4.6: The controller code and template markup to support the Index RESTful action. The controller method just retrieves all the movies in the Movies table using the all method introduced in the previous section, and assigns it to the @movies instance variable. Recall from the tour of a Rails app in Chapter 2 that instance variables defined in controller actions are available to views; line 12 of index.html.haml iterates over the collection @movies using each . There are three things to notice about this simple template. First, the columns in the table header ( th ) just have static text describing the table columns, but the columns in the table body ( td ) use Haml’s = syntax to indicate that the tag content should be evaluated as Ruby code, with the result substituted into the HTML document. In this case, we are using the attribute getters on Movie objects supplied by ActiveRecord . Sanitization Haml’s = syntax sanitizes the result of evaluating the Ruby code before inserting it into the HTML output, to help thwart cross-site scripting and similar attacks described in Chapter 12 . Second, we’ve given the table of movies the HTML ID movies . We will use this later for visually styling the page using CSS, as we learned about in Chapter 2 . Third is the call in line 17 to link_to , one of many helper methods provided by ActionView for creating views. As its documentation states, the first argument is a string that will appear as a link (clickable text) on the page and the second argument is used to create the URI that will become the actual link target. This argument can take several forms; the form we’ve used takes advantage of the URI helper movie_path() (as shown by rake routes for the show action), which takes as its argument an instance of a RESTful resource (in this case an instance of Movie ) and generates the RESTful URI for the Show RESTful route for that object. This behavior is a nice illustration of reflection and metaprogramming in the service of conciseness. As rake routes reminds you, the Show action for a movie is expressed by a URI /movies/:id where :id is the movie’s primary key in the Movies table, so that’s what the link target created by link_to will look like. To verify this, restart the application ( rails server in the app’s root directory) and visit , the URI corresponding to the index action. If all is well, you should see a list of any movies in the database. If you use your browser’s View Source option to look at the generated source, you can see that the links generated by link_to
Image of page 148
Image of page 149

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors