Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass collections to Rails partials that contain a block?
    primarykey
    data
    text
    <p>How to pass collections to Rails partials that contain a block. (Or, how to get around Rails magic).</p> <p>I have a feeling I'm think about this from the wrong angle. Can anyone point me in teh right direction?</p> <p>Say I have an index with two repeated blocks, like so:</p> <pre><code>&lt;div class="content"&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;COL1&lt;/th&gt; &lt;th&gt;COL2&lt;/th&gt; &lt;th&gt;COL3&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;% @collectionA.each do |a| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= a.col1 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= a.col2 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= a.col3 %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;tr&gt;&lt;%= will_paginate @collectionA %&gt;&lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;br /&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;COL1&lt;/th&gt; &lt;th&gt;COL2&lt;/th&gt; &lt;th&gt;COL3&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;% @collectionB.each do |b| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= b.col1 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= b.col2 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= b.col3 %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;tr&gt;&lt;%= will_paginate @collectionA %&gt;&lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; </code></pre> <p>My first round of DRYing up might look something like this.</p> <pre><code>... &lt;tbody&gt; &lt;%= render :partial =&gt; 'collections', :collection =&gt; @collectionA %&gt; &lt;/tbody&gt; &lt;%= will_paginate @collectionA %&gt; ... &lt;tbody&gt; &lt;%= render :partial =&gt; 'collections', :collection =&gt; @collectionb %&gt; &lt;/tbody&gt; &lt;%= will_paginate @collectionB %&gt; ... </code></pre> <p>But what if I need to move <code>will_paginate</code> into the partial as well, so that I can ajaxify it.</p> <p>If I only had one block, I would do</p> <pre><code>&lt;tbody&gt; &lt;%= render :partial =&gt; 'collection' %&gt; &lt;/tbody&gt; </code></pre> <p>and in the partial</p> <pre><code>&lt;% @collection.each do |col| %&gt; STUFF &lt;% end %&gt; &lt;%= will_paginate @collection %&gt; </code></pre> <p>But if I have two blocks, how can I pass @collection-A and @collection-B into the partial?</p> <p>Or am I looking at this the wrong way? Thanks for any pointers.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload