0

I am really confused with how rendered paginated objects work. Take this for example:

When I run

<%= render :partial => 'feeds/share_item', collection: @share_items %>
<%= will_paginate @share_items %>

I can go to _share_item.html.erb and inside of it I can do this...

<%= link_to "Delete", share_item, method: :delete, 
data: { confirm: "Are you sure you want to delete this?" }, 
class: "btn-link", title: share_item.content %>

...and it will have the Delete link and all goes well.

But, while still in _share_item.html.erb, if I replace that with...

<%= render :partial => 'shared/delete' %>

...I get this:

undefined local variable or method 'share_item' for #<#<Class:0xaf0b888>:0xb15cb3c>

Why?

I have been trying to resolve this issue for quite some time, because I have a lot of forms and links cluttered in my partial. I want to be able to render said forms and links via another partial.

Thanks for your help.

Nathan Couch
  • 222
  • 1
  • 12

1 Answers1

1

Right now, the variable share_item is empty, as it has not been supplied to the partial. You would need to do something like this:

<% @share_items.each do |share_item| %>
  <%= render :partial => 'shared/delete', :locals => { :share_item => share_item } %>
<% end %>

Or, the slightly more terse

<% @share_items.each do |share_item| %>
  <%= render 'shared/delete', share_item: share_item %>
<% end %>

Or, even better:

<%= render 'shared/delete',  collection: @share_items %>

You can find more info here.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88