Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 - rendering a collection with a partial name different to the variable

I have the following partial posts/post_preview in my project:

.post
  %h2= link_to(post.title, post)
  .post-body= simple_format(post.body)
  %ul
    - post.tags.each do |tag|
      %li= tag

I want to render this partial for a collection name @posts, like so:

render partial: 'post_preview', collection: @posts

However, render by default passes each member of the collection as a local variable named after the partial. Is there any way I can change this so each member of @posts is passed into the partial in a variable named post?

like image 419
Richard Stokes Avatar asked Apr 15 '14 05:04

Richard Stokes


1 Answers

http://guides.rubyonrails.org/layouts_and_rendering.html Check this for changing the name Let say you have products and want to use as item then just do like

<%= render partial: "product", collection: @products, as: :item %>

i recommend you to check this too. http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html

like image 155
Sabyasachi Ghosh Avatar answered Sep 28 '22 18:09

Sabyasachi Ghosh