简体   繁体   中英

What is the use of partials in Rails?

In Rails why would I ever use partials? Explain it as if I'm 5 years old. I simply do not get why anyone would ever use them.

First thing , Please have a read here Official Rails guide for partials .

Now some benefits

  1. It keeps your view clean and systematic, DRY Philosphy .

  2. The most important where partial comes into picture is when you want to reuse some of the component amongst various view. Usually developer create a kind of shared/common folder where partial sits and are used amongst various view.

  3. It's also easy to conditionally load partials using Rails' “if” or “unless” statements

  4. It is beneficial where a template needs to iterate over a collection and render a sub template for each of the elements.

  5. Your different partials can also have different layouts .

  6. The Partial API here list all the various methods which will make you understand it's benefit.

  7. Separating your view into partials can also help in in proper fragment caching ( Fragment Caching ) of some portion of you webpage. Better management.

  8. If you are into Metaprogramming then you can add that flavor in your partials too, by creating dynamic helpers. As Stackoverflow answer here

They are a handy way to avoid repeating yourself.

For example, you may have several pages that display a menu. Instead of repeating the markup for the menu in every view, you just throw it in a partial and render on every page.

There are other cases where complex views are made more manageable by breaking them into several partials.

You normally use partials to reuse code: let's say you have a list of posts and each post has a small preview with image, title and excerpt. In your blog homepage you have a list of posts, when you group posts by year, you show a list of posts, when you search for a term you have to display a list of posts.

Instead of repeating the logic to display a post preview, you can move that logic to a partial and keep referring to that whenever you need it. You can keep your code DRY and write less code. Moreover, if you realize you want to add something new, you can just change the partial instead of going and hunting for the templates which display the post previews all over your application.

Well, for the exact same reason that you want to use methods, which is reusing code. Say you have a status area in your application, that you want to show in different places. You can just put the view code for that status area in a partial and then use that partial on the corresponding pages.

Since partials can also take parameters, they make it very easy to reuse view code. Also, you can make partials dedicated for certain models of your application. This way, you can just call render @model and the correct partial is picked by naming conventions.

A partial allows you to separate layout code out into a file which will be reused throughout the layout and/or multiple other layouts.

For example, you might have a login form that you want to display on 10 different pages on your site. Rather than writing the form code 10 times, you can write it once in a partial, and in each of the 10 pages, simply include that partial at the appropriate place in the layout. If necessary, you can pass local instance variables to the partial to make them available to it.

That way, if you need to change the form, you only need to change the code in the partial, one time, rather than changing it 10 times across all of your layouts.

Here is the guide on partials: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

And more discussion on getting data/variables into partials: Pass a variable into a partial, rails 3?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM