This project is archived and is in readonly mode.

#2467 ✓invalid

Parameterized partials

Reported by ebelin | April 9th, 2009 @ 07:23 PM | in 2.x


I suggest to parameterize 'smart' partials rendering like that:

<%= render :partial => @cart.products, :context => 'cart' %>

which would be an equivalent to something like that:

<%= render :partial => 'products/product_for_cart', :collection => @cart.products %>


In the snippet below all product, category and cart pages (in a standard 'category has many products' scenario) display the same views/products/_product.html.erb partial.

# views/products/show.html.erb
<%= render @products, :locals => {:context => 'product'} %>

# views/categories/show.html.erb
<%= render @category.products, :locals => {:context => 'category'} %>

# views/shopping_cart/show.html.erb
<%= render @cart.products, :locals => {:context => 'cart'} %>

This leads to overly complicated views/products/_product.html.erb logic of what and how to display that depends on context. I found that readability is more important than DRY sometimes and I usually end up with a series of partials:


and render them appropriately:

<%= render :partial => 'products/category_product', :collection => @category.products %>

I suggest to improve smart partials by parameterizing their rendering a-la:

<%= render :partial => @cart.products, :context => 'cart' %>

which would be an equivalent to

<%= render :partial => 'products/product_for_cart', :collection => @cart.products %>

Comments and changes to this ticket

  • Steve St. Martin

    Steve St. Martin April 16th, 2010 @ 12:28 AM

    • Assigned user set to “Ryan Bigg”

    Not sure I 100% understand the usecase here why if readability was more important than dry how adding :context makes it anymore readable.

    I would use the following, it gives you the separation you require, no added functionality, and reads cleanly showing its intent. the only sacrifice is the partials are not all contained under views/products. However i would argue that if the partials are so heavily different they cannot be shared, they are likely tied to the section they are contained within to begin with.

      # views/shopping_cart/show.html.erb -> /views/shopping_cart/_product.html.erb
      <%= render :partial => 'product', :collection => @cart.products %>
      # views/categories/show.html.erb -> /views/categories/_product.html.erb
      <%= render :partial => 'product', :collection => @category.products %>
      # views/products/show.html.erb -> /views/products/_product.html.erb
      <%= render :partial => 'product', :collection => @products %>

    suggest marking as invalid

  • Ryan Bigg

    Ryan Bigg April 16th, 2010 @ 12:38 AM

    • State changed from “new” to “invalid”

    That final example you can just render @products.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket