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 -%>

About Paul

Paul works for Kyan web design agency in Surrey, UK as a Ruby on Rails developer.

Follow Paul on Twitter

Email: paulsturgess [at] gmail.com

Read more articles in the archive →

Comments...

  • thanks buddy!

    Unni at 24 Jul 08 at 02:32

  • thanks buddy!

    Dunni at 25 Nov 11 at 22:01

  • thanks buddy!

    Runni at 27 Dec 11 at 11:15

Got something to say?