Rails partial hacking: pass 2 records per partial? Or a better idea?










0















I'm creating a view for printing labels. I've HTML'd / CSS'd a table with the right dimensions for 2 columns of 5 labels:



<div id="inventory_labels">
<table class="table-bordered" id="avery8163">
<thead>
<tr>
<td style='width: .155in;'></td>
<td style='width: 4in;'></td>
<td style='width: .19in;'></td>
<td style='width: 4in;'></td>
<td style='width: .155in;'></td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5"></td>
</tr>
</tfoot>
<tbody>
<% 40.times do # just a placeholder for the actual loop / partial %>
<tr>
<td></td>
<td>LABEL #1 (should be a record)</td>
<td></td>
<td>LABEL #2 (should be a record)</td>
<td></td>
</tr>
<% end %>
</tbody>
</table>
</div>


And the SCSS:



#inventory_labels 
width: 8.5in;
margin-right: auto;
margin-left: auto;


table#avery8163
table-layout: fixed;
width: 8.5in;

thead, tfoot
td
height: .5in;



tbody
td
height: 2in;




@media print
table#avery8163
thead
display: table-header-group;


tfoot
display: table-footer-group;





I'd like to use a partial to loop over the records in the <tbody>, but I need to loop over 2 at a time, one for each "LABEL".



Any suggestions or ideas?



I did play with formatting this as divs (like this strategy from the dark ages) instead of table columns but the math got real messy since the page-margin and gutters don't line up well.



FIRST RUN



This is really ugly, right?



@counts is the ActiveRecord array of records I want to loop through, defined in the controller.



<tbody>
<% n = 0 %>
<% @loop.times do # @loop = (@counts.count/2).ceil %>
<tr>
<td></td>
<td>
<%= render partial: 'label', locals: count: @counts[n] %>
<% n += 1 %>
</td>
<td></td>
<td>
<% if @counts[n].present? %>
<%= render partial: 'label', locals: count: @counts[n] %>
<% end %>
<% n += 1 %>
</td>
<td></td>
</tr>

<% end %>
</tbody>









share|improve this question
























  • Is that the actual code? do you want 40 rows with "empty | LABEL #1 | empty | LABEL #2 | empty"? or do you want the labels to be correlative until LABEL #80?

    – arieljuod
    Nov 15 '18 at 4:11











  • I don't understand your edit, what's @counts comming from? what's in it? what does the _label partial look like?

    – arieljuod
    Nov 15 '18 at 4:12











  • @arieljuod, added some clarification. TL;DR: #1 40.times was just a placeholder for whatever loop or partial I managed to construct. #2 @counts is an ActiveRecord collection of records. The partial code is just more ERB, and not really applicable.

    – Chiperific
    Nov 15 '18 at 4:30















0















I'm creating a view for printing labels. I've HTML'd / CSS'd a table with the right dimensions for 2 columns of 5 labels:



<div id="inventory_labels">
<table class="table-bordered" id="avery8163">
<thead>
<tr>
<td style='width: .155in;'></td>
<td style='width: 4in;'></td>
<td style='width: .19in;'></td>
<td style='width: 4in;'></td>
<td style='width: .155in;'></td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5"></td>
</tr>
</tfoot>
<tbody>
<% 40.times do # just a placeholder for the actual loop / partial %>
<tr>
<td></td>
<td>LABEL #1 (should be a record)</td>
<td></td>
<td>LABEL #2 (should be a record)</td>
<td></td>
</tr>
<% end %>
</tbody>
</table>
</div>


And the SCSS:



#inventory_labels 
width: 8.5in;
margin-right: auto;
margin-left: auto;


table#avery8163
table-layout: fixed;
width: 8.5in;

thead, tfoot
td
height: .5in;



tbody
td
height: 2in;




@media print
table#avery8163
thead
display: table-header-group;


tfoot
display: table-footer-group;





I'd like to use a partial to loop over the records in the <tbody>, but I need to loop over 2 at a time, one for each "LABEL".



Any suggestions or ideas?



I did play with formatting this as divs (like this strategy from the dark ages) instead of table columns but the math got real messy since the page-margin and gutters don't line up well.



FIRST RUN



This is really ugly, right?



@counts is the ActiveRecord array of records I want to loop through, defined in the controller.



<tbody>
<% n = 0 %>
<% @loop.times do # @loop = (@counts.count/2).ceil %>
<tr>
<td></td>
<td>
<%= render partial: 'label', locals: count: @counts[n] %>
<% n += 1 %>
</td>
<td></td>
<td>
<% if @counts[n].present? %>
<%= render partial: 'label', locals: count: @counts[n] %>
<% end %>
<% n += 1 %>
</td>
<td></td>
</tr>

<% end %>
</tbody>









share|improve this question
























  • Is that the actual code? do you want 40 rows with "empty | LABEL #1 | empty | LABEL #2 | empty"? or do you want the labels to be correlative until LABEL #80?

    – arieljuod
    Nov 15 '18 at 4:11











  • I don't understand your edit, what's @counts comming from? what's in it? what does the _label partial look like?

    – arieljuod
    Nov 15 '18 at 4:12











  • @arieljuod, added some clarification. TL;DR: #1 40.times was just a placeholder for whatever loop or partial I managed to construct. #2 @counts is an ActiveRecord collection of records. The partial code is just more ERB, and not really applicable.

    – Chiperific
    Nov 15 '18 at 4:30













0












0








0








I'm creating a view for printing labels. I've HTML'd / CSS'd a table with the right dimensions for 2 columns of 5 labels:



<div id="inventory_labels">
<table class="table-bordered" id="avery8163">
<thead>
<tr>
<td style='width: .155in;'></td>
<td style='width: 4in;'></td>
<td style='width: .19in;'></td>
<td style='width: 4in;'></td>
<td style='width: .155in;'></td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5"></td>
</tr>
</tfoot>
<tbody>
<% 40.times do # just a placeholder for the actual loop / partial %>
<tr>
<td></td>
<td>LABEL #1 (should be a record)</td>
<td></td>
<td>LABEL #2 (should be a record)</td>
<td></td>
</tr>
<% end %>
</tbody>
</table>
</div>


And the SCSS:



#inventory_labels 
width: 8.5in;
margin-right: auto;
margin-left: auto;


table#avery8163
table-layout: fixed;
width: 8.5in;

thead, tfoot
td
height: .5in;



tbody
td
height: 2in;




@media print
table#avery8163
thead
display: table-header-group;


tfoot
display: table-footer-group;





I'd like to use a partial to loop over the records in the <tbody>, but I need to loop over 2 at a time, one for each "LABEL".



Any suggestions or ideas?



I did play with formatting this as divs (like this strategy from the dark ages) instead of table columns but the math got real messy since the page-margin and gutters don't line up well.



FIRST RUN



This is really ugly, right?



@counts is the ActiveRecord array of records I want to loop through, defined in the controller.



<tbody>
<% n = 0 %>
<% @loop.times do # @loop = (@counts.count/2).ceil %>
<tr>
<td></td>
<td>
<%= render partial: 'label', locals: count: @counts[n] %>
<% n += 1 %>
</td>
<td></td>
<td>
<% if @counts[n].present? %>
<%= render partial: 'label', locals: count: @counts[n] %>
<% end %>
<% n += 1 %>
</td>
<td></td>
</tr>

<% end %>
</tbody>









share|improve this question
















I'm creating a view for printing labels. I've HTML'd / CSS'd a table with the right dimensions for 2 columns of 5 labels:



<div id="inventory_labels">
<table class="table-bordered" id="avery8163">
<thead>
<tr>
<td style='width: .155in;'></td>
<td style='width: 4in;'></td>
<td style='width: .19in;'></td>
<td style='width: 4in;'></td>
<td style='width: .155in;'></td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5"></td>
</tr>
</tfoot>
<tbody>
<% 40.times do # just a placeholder for the actual loop / partial %>
<tr>
<td></td>
<td>LABEL #1 (should be a record)</td>
<td></td>
<td>LABEL #2 (should be a record)</td>
<td></td>
</tr>
<% end %>
</tbody>
</table>
</div>


And the SCSS:



#inventory_labels 
width: 8.5in;
margin-right: auto;
margin-left: auto;


table#avery8163
table-layout: fixed;
width: 8.5in;

thead, tfoot
td
height: .5in;



tbody
td
height: 2in;




@media print
table#avery8163
thead
display: table-header-group;


tfoot
display: table-footer-group;





I'd like to use a partial to loop over the records in the <tbody>, but I need to loop over 2 at a time, one for each "LABEL".



Any suggestions or ideas?



I did play with formatting this as divs (like this strategy from the dark ages) instead of table columns but the math got real messy since the page-margin and gutters don't line up well.



FIRST RUN



This is really ugly, right?



@counts is the ActiveRecord array of records I want to loop through, defined in the controller.



<tbody>
<% n = 0 %>
<% @loop.times do # @loop = (@counts.count/2).ceil %>
<tr>
<td></td>
<td>
<%= render partial: 'label', locals: count: @counts[n] %>
<% n += 1 %>
</td>
<td></td>
<td>
<% if @counts[n].present? %>
<%= render partial: 'label', locals: count: @counts[n] %>
<% end %>
<% n += 1 %>
</td>
<td></td>
</tr>

<% end %>
</tbody>






ruby-on-rails partials






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 4:29







Chiperific

















asked Nov 15 '18 at 3:32









ChiperificChiperific

1,3971228




1,3971228












  • Is that the actual code? do you want 40 rows with "empty | LABEL #1 | empty | LABEL #2 | empty"? or do you want the labels to be correlative until LABEL #80?

    – arieljuod
    Nov 15 '18 at 4:11











  • I don't understand your edit, what's @counts comming from? what's in it? what does the _label partial look like?

    – arieljuod
    Nov 15 '18 at 4:12











  • @arieljuod, added some clarification. TL;DR: #1 40.times was just a placeholder for whatever loop or partial I managed to construct. #2 @counts is an ActiveRecord collection of records. The partial code is just more ERB, and not really applicable.

    – Chiperific
    Nov 15 '18 at 4:30

















  • Is that the actual code? do you want 40 rows with "empty | LABEL #1 | empty | LABEL #2 | empty"? or do you want the labels to be correlative until LABEL #80?

    – arieljuod
    Nov 15 '18 at 4:11











  • I don't understand your edit, what's @counts comming from? what's in it? what does the _label partial look like?

    – arieljuod
    Nov 15 '18 at 4:12











  • @arieljuod, added some clarification. TL;DR: #1 40.times was just a placeholder for whatever loop or partial I managed to construct. #2 @counts is an ActiveRecord collection of records. The partial code is just more ERB, and not really applicable.

    – Chiperific
    Nov 15 '18 at 4:30
















Is that the actual code? do you want 40 rows with "empty | LABEL #1 | empty | LABEL #2 | empty"? or do you want the labels to be correlative until LABEL #80?

– arieljuod
Nov 15 '18 at 4:11





Is that the actual code? do you want 40 rows with "empty | LABEL #1 | empty | LABEL #2 | empty"? or do you want the labels to be correlative until LABEL #80?

– arieljuod
Nov 15 '18 at 4:11













I don't understand your edit, what's @counts comming from? what's in it? what does the _label partial look like?

– arieljuod
Nov 15 '18 at 4:12





I don't understand your edit, what's @counts comming from? what's in it? what does the _label partial look like?

– arieljuod
Nov 15 '18 at 4:12













@arieljuod, added some clarification. TL;DR: #1 40.times was just a placeholder for whatever loop or partial I managed to construct. #2 @counts is an ActiveRecord collection of records. The partial code is just more ERB, and not really applicable.

– Chiperific
Nov 15 '18 at 4:30





@arieljuod, added some clarification. TL;DR: #1 40.times was just a placeholder for whatever loop or partial I managed to construct. #2 @counts is an ActiveRecord collection of records. The partial code is just more ERB, and not really applicable.

– Chiperific
Nov 15 '18 at 4:30












1 Answer
1






active

oldest

votes


















1














Seeing that it's not a problem to use your collection as an array, you can use in_groups_of method https://api.rubyonrails.org/classes/Array.html#method-i-in_groups_of



<% @objects.in_groups_of(2, false) do |obj1, obj2| %>
<tr>
<td></td>
<td><%= #something with obj1 %></td>
<td></td>
<td><%= #something with obj2, you should check if it's not nil though if the array has an odd length %></td>
<td</td>
</tr>
<% end %>





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312033%2frails-partial-hacking-pass-2-records-per-partial-or-a-better-idea%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Seeing that it's not a problem to use your collection as an array, you can use in_groups_of method https://api.rubyonrails.org/classes/Array.html#method-i-in_groups_of



    <% @objects.in_groups_of(2, false) do |obj1, obj2| %>
    <tr>
    <td></td>
    <td><%= #something with obj1 %></td>
    <td></td>
    <td><%= #something with obj2, you should check if it's not nil though if the array has an odd length %></td>
    <td</td>
    </tr>
    <% end %>





    share|improve this answer



























      1














      Seeing that it's not a problem to use your collection as an array, you can use in_groups_of method https://api.rubyonrails.org/classes/Array.html#method-i-in_groups_of



      <% @objects.in_groups_of(2, false) do |obj1, obj2| %>
      <tr>
      <td></td>
      <td><%= #something with obj1 %></td>
      <td></td>
      <td><%= #something with obj2, you should check if it's not nil though if the array has an odd length %></td>
      <td</td>
      </tr>
      <% end %>





      share|improve this answer

























        1












        1








        1







        Seeing that it's not a problem to use your collection as an array, you can use in_groups_of method https://api.rubyonrails.org/classes/Array.html#method-i-in_groups_of



        <% @objects.in_groups_of(2, false) do |obj1, obj2| %>
        <tr>
        <td></td>
        <td><%= #something with obj1 %></td>
        <td></td>
        <td><%= #something with obj2, you should check if it's not nil though if the array has an odd length %></td>
        <td</td>
        </tr>
        <% end %>





        share|improve this answer













        Seeing that it's not a problem to use your collection as an array, you can use in_groups_of method https://api.rubyonrails.org/classes/Array.html#method-i-in_groups_of



        <% @objects.in_groups_of(2, false) do |obj1, obj2| %>
        <tr>
        <td></td>
        <td><%= #something with obj1 %></td>
        <td></td>
        <td><%= #something with obj2, you should check if it's not nil though if the array has an odd length %></td>
        <td</td>
        </tr>
        <% end %>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 4:55









        arieljuodarieljuod

        7,38311221




        7,38311221





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312033%2frails-partial-hacking-pass-2-records-per-partial-or-a-better-idea%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto