A partial is Rails name for a reusable chunk of a view When similar content

A partial is rails name for a reusable chunk of a

This preview shows page 172 - 174 out of 517 pages.

explains, the partial is also the basic unit of view updating for JavaScript-enabled pages. A partial is Rails’ name for a reusable chunk of a view. When similar content must appear in different views, putting that content in a partial and “including” it in the separate files helps DRY out repetition. For example, in Chapter 4 , Figure 4.14 noted the similarities between the new and edit actions, both of whose views display the same form for entering movie information—not very DRY. Figure 5.1 captures this common view code in a partial and shows how to modify the existing new.html.haml and edit.html.haml to use it. 1 -# in _movie_form.html.haml (the partial) 2 3 = label :movie, :title, ’Title’ 4 = text_field :movie, ’title’ 5 6 = label :movie, :rating, ’Rating’ 7 = select :movie, :rating, Movie.all_ratings 8 9 = label :movie, :release_date, ’Released On’ 10 = date_select :movie, :release_date 1 -# new.html.haml using partial 2 3 %h2 Create New Movie 4 5 = form_tag ’/movies’, :method => :post do 6 = render :partial => ’movie_form’ 7 = submit_tag ’Save Changes’ 1 -# edit.html.haml using partial 2 3 %h2 Edit Existing Movie 4 5 = form_tag movie_path(@movie), :method => :put do 6 = render :partial => ’movie_form’ 7 = submit_tag ’Update Movie Info’
Image of page 172
Figure 5.1: A partial (top) that captures the form elements common to both the new and edit templates. The modified new and edit templates both use the partial (line 6 in both snippets), but line 5 is different between the two templates because new and edit submit to different actions. (Use the Pastebin service to copy-and-paste this code.) Partials rely heavily on convention over configuration. Their names must begin with an underscore (we used _movie_form.html.haml ) which is absent from the code that references the partial. If a partial is not in the same directory as the view that uses it, ’layouts/footer’ would cause Rails to look for app/views/layouts/_footer.html.haml . A partial can access all the same instance variables as the view that includes it. A particularly nice use of a partial is to render a table or other collection in which all elements are the same, as Figure 5.2 demonstrates. 1 -# A single row of the All Movies table 2 %tr 3 %td= movie.title 4 %td= movie.rating 5 %td= movie.release_date 6 %td= link_to "More about #{movie.title}", movie_path(movie) Figure 5.2: If this partial is saved as views/movies/_movie.html.haml , lines 12–17 of our original index.html.haml view (Figure 4.6 ) can be replaced with the single line = render :partial=>’movie’, :collection=>@movies . By convention over configuration, the name of the partial without the leading underscore (in this case, movie ) is available as a local variable in the partial that is set to each element of @movies in turn. Partials are simple and straightforward, but the mechanisms provided by Rails for DRYing out models and controllers are more subtle and sophisticated. It’s common in SaaS apps to want to enforce certain validity constraints on a given type of model object or constraints on when certain actions can be performed. For example, when a new movie is added to RottenPotatoes, we may want to check that the
Image of page 173
Image of page 174

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