0

How to pass collections to Rails partials that contain a block. (Or, how to get around Rails magic).

I have a feeling I'm think about this from the wrong angle. Can anyone point me in teh right direction?

Say I have an index with two repeated blocks, like so:

<div class="content">
  <table>
    <thead>
      <tr>
        <th>COL1</th>
        <th>COL2</th>
        <th>COL3</th>
      </tr>
    </thead>
    <tbody>
      <% @collectionA.each do |a| %>
        <tr>
          <td><%= a.col1 %></td> 
          <td><%= a.col2 %></td>
          <td><%= a.col3 %></td>
        </tr>
      <% end %>
        <tr><%= will_paginate @collectionA %></tr>
    </tbody>
  </table>

  <br />

  <table>
    <thead>
      <tr>
        <th>COL1</th>
        <th>COL2</th>
        <th>COL3</th>
      </tr>
    </thead>
    <tbody>
      <% @collectionB.each do |b| %>
        <tr>
          <td><%= b.col1 %></td> 
          <td><%= b.col2 %></td>
          <td><%= b.col3 %></td>
        </tr>
      <% end %>
        <tr><%= will_paginate @collectionA %></tr>
    </tbody>
  </table>
</div>

My first round of DRYing up might look something like this.

...
<tbody>
  <%= render :partial => 'collections', :collection => @collectionA %>
</tbody>
<%= will_paginate @collectionA %>

...

<tbody>
  <%= render :partial => 'collections', :collection => @collectionb %>
</tbody>
<%= will_paginate @collectionB %>
...

But what if I need to move will_paginate into the partial as well, so that I can ajaxify it.

If I only had one block, I would do

<tbody>
  <%= render :partial => 'collection' %>
</tbody>

and in the partial

<% @collection.each do |col| %>
  STUFF
<% end %>
<%= will_paginate @collection %>

But if I have two blocks, how can I pass @collection-A and @collection-B into the partial?

Or am I looking at this the wrong way? Thanks for any pointers.

Andy Harvey
  • 12,333
  • 17
  • 93
  • 185

1 Answers1

4

Try:

<%= render :partial => 'collection', :locals => {:collection => collectionA} %>

In partial just remove @:

<% collection.each do |col| %>
  STUFF
<% end %>
<%= will_paginate collection %>
jdoe
  • 15,665
  • 2
  • 46
  • 48
  • thanks, this looks perfect. I'd actually experimented with this but discounted it. Seem to remember thinking that I could pass single variables but not collections/ arrays. Must have been doing something wrong, as it seems to be working now! Quick question: How would I pass pagination `:param_name` in using this method. I assume `..:locals=>{:param_name => :my_page} %>`. Then in the partial `<%= will_paginate collection, :param_name => param_name %>`. But this doesn't seem to be working for me. Am I doing something wrong? – Andy Harvey May 14 '12 at 11:40
  • Yes, you have to build the following locals: `:locals=>{:collection => collectionA, :param_name => :my_page}` and your `will_paginate`-related suggestion seems correct. Did you try it? It should work. – jdoe May 14 '12 at 11:46
  • I've been playing with this a while, but still having some problems. Everything works with a standard setup - call the partial: `<%= render :partial => 'users/user' %>` in the partial: `<% @users.each do |user| %>...<% end %> <%= will_paginate @users %>. But as soon as I alter this to pass a local - call the partial: `<%= render :partial => 'users/user', :locals => => {:users => @users} %>` in the partial: `<% users.each do |user| %>...<% end %> <%= will_paginate users %> - it stops working. Does this happen for anyone else? Or just me? – Andy Harvey May 14 '12 at 17:37
  • I have a suspicion my issue is linked to my ajax implementation. I'm following this Railscast http://railscasts.com/episodes/240-search-sort-paginate-with-ajax?view=asciicast. I guess my ajax calls are expecting to find the @instance_variables specified in my index controller, which are no longer present. Is there a better way to ajaxify this? Appreciate any pointers to useful info. – Andy Harvey May 14 '12 at 17:40
  • I guess you need to post new question since your last comment-question has nothing to do with passing variables into partials. – jdoe May 14 '12 at 17:52
  • Thanks jdoe, I've spent the past day trying to understand this what's happening here, but I'm still scratching my head! So, a new question is up at http://stackoverflow.com/questions/10597904/how-to-pass-locals-to-will-paginate-in-a-partial. Appreciate any ideas you may have. – Andy Harvey May 15 '12 at 09:33