Self Check 431 Why are where and find class methods rather than instance

Self check 431 why are where and find class methods

This preview shows page 146 - 148 out of 517 pages.

Self-Check 4.3.1. Why are where and find class methods rather than instance methods? Instance methods operate on one instance of the class, but until we look up one or more objects, we have no instance to operate on. Self-Check 4.3.2. Do Rails models acquire the methods where and find via (a) inheritance or (b) mix- in? (Hint: check the movie.rb file.) (a) they inherit from ActiveRecord::Base . 4.4 Controllers and Views We’ll complete our tour by creating some views to support the CRUD actions we just learned about. The RESTful routes we defined previously ( rake routes to remind yourself what they are) expect the controller to provide actions for index , show , new / create (recall from Chapter 2 that creating an object requires two interactions with the user), edit / update (similarly), and destroy . Starting with the two easiest actions, index should display a list of all movies, allowing us to click on each one, and show should display details for the movie we click on. For the index action, we know from the walk-through examples in Section 4.3 that Movie.all returns a collection of all the movies in the Movies table. Thus we need a controller method that sets up this collection and an HTML view that displays it. By convention over configuration, Rails expects the following for a method implementing the Show RESTful action on a Movie resource (note the uses of singular vs. plural and of CamelCase vs. snake_case ):
Image of page 146
The model code is in class Movie , which inherits from ActiveRecord::Base and is defined in app/models/movie.rb The controller code is in class MoviesController , defined in app/controllers/movies_controller.rb (note that the model’s class name is pluralized to form the controller file name.) Your app’s controllers all inherit from your app’s root controller ApplicationController (in app/controllers/application_controller.rb ), which in turn inherits from ActionController::Base . Each instance method of the controller is named using snake_lower_case according to the action it handles, so the show method would handle the Show action The Show view template is in app/views/movies/show.html.haml , with the .haml extension indicating use of the Haml renderer. Other extensions include .xml for a file containing XML Builder code (as we saw in Section 3.6 ), .erb (which we’ll meet shortly) for Rails’ built-in Embedded Ruby renderer, and many others. The Rails module that choreographs how views are handled is ActionView::Base . Since we’ve been using the Haml markup for our views (recall we added the Haml gem to our Gemfile dependencies), our view files will have names ending in .html.haml . Therefore, to implement the Index RESTful action, we must define an index action in app/controllers/movies_controller.rb and a view template in app/views/movies/index.html.haml . Create these two files using Figure 4.6 (you will need to create the intermediate directory app/views/movies/ ).
Image of page 147
Image of page 148

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