How to pass an instance variable to a rendered partial in Ruby on Rails
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 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
1 comment made
thanks buddy!
Got something to say?