How to pass an instance variable to a rendered partial in Ruby on Rails

Posted on 28 April 2006

Passing a variable to a partial means you can be clever about re-using your code to output different collections of data in the same way.

Essentially you just write:


<%= render :partial => '/path/to/_partial', :locals => { :items => @items } %>

The local variable items now makes each element of @items available in the partial. e.g.


<% for item in items -%>
  do something...
<% end -%>

Comments left...

  • thanks buddy!

    Unni at 24 Jul 08 at 02:32

Got something to say?