Skip to content

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

View the snippets archive ››

1 comment made

Unni commented on 24 Jul 08 at 02:32

thanks buddy!

Got something to say?

About

Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.

Contact

my name at gmail.com

More snippets

Take a look in the archive

Need a website?

Contact my employer. Make sure to check out our portfolio of work.

Hosting

I recommend hostingrails.com