Screencast 511 How model validations interact with controllers and views The

Screencast 511 how model validations interact with

This preview shows page 176 - 178 out of 517 pages.

Screencast 5.1.1: How model validations interact with controllers and views The form helpers in our views use the errors object to discover which fields caused validation errors and apply the special CSS class field-with-errors to any such fields. By including selectors for that class in app/assets/stylesheets/application.css , we can visually highlight the affected fields for the user’s benefit.
Image of page 176
Figure 5.5: The various points at which you can “hook into” the lifecycle of an ActiveRecord model object. All ActiveRecord operations that modify the database ( update , create , and so on) all eventually call save , so a before_save callback can intercept every change to the database. See this Rails Guide for additional details and examples. 1 class Movie < ActiveRecord::Base 2 before_save :capitalize_title 3 def capitalize_title 4 self.title = self.title.split(/\s+/).map(&:downcase). 5 map(&:capitalize).join(’ ’) 6 end 7 end 8 # now try in console: 9 m = Movie.create!(:title => ’STAR wars’, :release_date => ’27-5- 1977’, :rating => ’PG’) 10 m.title # => "Star Wars" Figure 5.6: This before_save hook capitalizes each word of a movie title, downcases the rest of the word, and compresses multiple spaces between words to a single space, turning STAR wars into Star Wars (not necessarily the right behavior for movie titles, but useful for illustration). In fact, validations are just a special case of a more general mechanism, Active Record lifecycle callbacks , which allow you to provide methods that “intercept” a model object at various relevant points in its lifecycle. Figure 5.5 shows what callbacks are available; Figure 5.6 illustrates how to use this mechanism to “canonicalize” (standardize the format of) certain model fields before the model is saved. We will see another use of lifecycle callbacks when we discuss the Observer design pattern in Chapter 11 and caching in Chapter 12 . Analogous to a validation is a controller filter—a method that checks whether certain conditions are true before an action is run, or sets up common conditions that many actions rely on. If the conditions are not fulfilled, the filter can choose to “stop the show” by rendering a view template or redirecting to another action. If the filter allows the action to proceed, it will be the action’s responsibility to provide a response, as usual. As an example, an extremely common use of filters is to enforce the requirement that a user be logged in before certain actions can be performed. Assume for the moment that we have verified the identity of some user and stored her primary key (ID) in session[:user_id] to remember the fact that she has logged in. Figure 5.7 shows a filter that enforces that a valid user is logged in. In Section 5.2 we will show how to combine the before-filter with the other “moving parts” involved in dealing with logged-in users.
Image of page 177
Image of page 178

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