Phipsen
Phipsen

Reputation: 169

rails render partial template with collection not working

Found some similar answers, but no one seems to fit. I also read http://guides.rubyonrails.org/layouts_and_rendering.html, chapter 3.4.5, Rendering collections.

I'd like to render the following partial: (users/_user_append.html.erb)

<%= content_tag_for(:tr, user) do %>
  <td class="dim"><%= user.id %></td>
  <td><%= user.lastname %></td>
  <td><%= user.firstname %></td>
  <td><%= user.company %></td>
  <td><%= user.email %></td>
  <td class="fadeactions col-md-2">
    <%= link_to(content_tag(:i, nil, class: 'fa fa-minus-square-o'),
        remove_mailgroup_user_path(@mailgroup, user),
          :method => :delete, remote: true,
          :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
            toggle: "tooltip", title: "Remove user", placement: 'top'
          }
        )
     %>
  </td>
<% end %>

The call for rendering looks like:

<%= render partial: 'users/user_append', collection: @users, as: :user %>

...as: :user... should pass the collection to the partial, as far I understand. However, nothing appears in the browser. Who finds the bug? Thanks a lot for your help.

Upvotes: 1

Views: 1460

Answers (1)

davegson
davegson

Reputation: 8331

After some discussion we realised the problem was not in the way you call your partial, but rather in the way you defined the @users. As they weren't defined correctly, there was no way for the partial to receive the :user param.

The solution was to change

<%= render partial: '/users/user_append', collection: @users, as: :user %>

to

<%= render partial: '/users/user_append', collection: @mailgroup.users, as: :user %>

Upvotes: 1

Related Questions