Rendering a partial inside a loop











up vote
-2
down vote

favorite












I'm trying to render the following partial:



<% @accepted.each do |question| %>
<div class="questions-container__content">
<div class="questions-container__accepted-content">
...
</div>
<%= render 'question_buttons', collection: @accepted %>
</div>
<% end %>


with _question_buttons.html.erb:



<div class="links-container__button-group" id="link-buttons">
<%= link_to "View submission", coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<%= link_to "Edit", edit_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% if !question.accepted? %>
<%= link_to "Activate" , activate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% else %>
<%= link_to "Deactivate" , deactivate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
<% if current_user.admin? %>
<%= link_to (question.rejected ? "Restore" : "Reject"), reject_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
</div>


I get the following error:



undefined local variable or method `question' for #<#<Class:0x00007fece6998d08>:0x00007fed02072bb8>


What am I doing wrong here?










share|improve this question




























    up vote
    -2
    down vote

    favorite












    I'm trying to render the following partial:



    <% @accepted.each do |question| %>
    <div class="questions-container__content">
    <div class="questions-container__accepted-content">
    ...
    </div>
    <%= render 'question_buttons', collection: @accepted %>
    </div>
    <% end %>


    with _question_buttons.html.erb:



    <div class="links-container__button-group" id="link-buttons">
    <%= link_to "View submission", coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
    <%= link_to "Edit", edit_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
    <% if !question.accepted? %>
    <%= link_to "Activate" , activate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
    <% else %>
    <%= link_to "Deactivate" , deactivate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
    <% end %>
    <% if current_user.admin? %>
    <%= link_to (question.rejected ? "Restore" : "Reject"), reject_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
    <% end %>
    </div>


    I get the following error:



    undefined local variable or method `question' for #<#<Class:0x00007fece6998d08>:0x00007fed02072bb8>


    What am I doing wrong here?










    share|improve this question


























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      I'm trying to render the following partial:



      <% @accepted.each do |question| %>
      <div class="questions-container__content">
      <div class="questions-container__accepted-content">
      ...
      </div>
      <%= render 'question_buttons', collection: @accepted %>
      </div>
      <% end %>


      with _question_buttons.html.erb:



      <div class="links-container__button-group" id="link-buttons">
      <%= link_to "View submission", coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <%= link_to "Edit", edit_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <% if !question.accepted? %>
      <%= link_to "Activate" , activate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <% else %>
      <%= link_to "Deactivate" , deactivate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <% end %>
      <% if current_user.admin? %>
      <%= link_to (question.rejected ? "Restore" : "Reject"), reject_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <% end %>
      </div>


      I get the following error:



      undefined local variable or method `question' for #<#<Class:0x00007fece6998d08>:0x00007fed02072bb8>


      What am I doing wrong here?










      share|improve this question















      I'm trying to render the following partial:



      <% @accepted.each do |question| %>
      <div class="questions-container__content">
      <div class="questions-container__accepted-content">
      ...
      </div>
      <%= render 'question_buttons', collection: @accepted %>
      </div>
      <% end %>


      with _question_buttons.html.erb:



      <div class="links-container__button-group" id="link-buttons">
      <%= link_to "View submission", coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <%= link_to "Edit", edit_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <% if !question.accepted? %>
      <%= link_to "Activate" , activate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <% else %>
      <%= link_to "Deactivate" , deactivate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <% end %>
      <% if current_user.admin? %>
      <%= link_to (question.rejected ? "Restore" : "Reject"), reject_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
      <% end %>
      </div>


      I get the following error:



      undefined local variable or method `question' for #<#<Class:0x00007fece6998d08>:0x00007fed02072bb8>


      What am I doing wrong here?







      ruby-on-rails






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 8:41









      sawa

      128k27192296




      128k27192296










      asked Nov 9 at 0:05









      mxvx

      1028




      1028
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          I believe the issue is that you need to pass the question variable from each loop in the parent view through to the partial using locals which allows the partial to access it.



              <%= render 'question_buttons', locals: { question: question } %>





          share|improve this answer























          • does this work on your version of rails? <%= render 'question_buttons', question: question %>?
            – vee
            Nov 9 at 0:50










          • i don't see the use of collection variable either. why do you have it there?
            – vee
            Nov 9 at 0:51


















          up vote
          0
          down vote













          First:



          When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:



          <%= render 'question_buttons', collection: @accepted %>


          the partial question_buttons will be called for each item of @accepted array; that item will be available inside partial as question_buttons.



          If you want to use another name for the item, for example question, you need to call it as:



          <%= render 'question_buttons', collection: @accepted, as: :question %>


          Another option -- just rename the partial to question:



          <%= render 'question', collection: @accepted %>




          Second:



          In your code snippet rendering collection is called on each iteration of the loop over @accepted elements. If @accepted has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons partial needs to be rendered for each element of @accepted only once. In this case using collection param has no sense here. Just pass a local variable question into the partial:



          <%= render 'question_buttons', question: question %>





          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',
            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%2f53217982%2frendering-a-partial-inside-a-loop%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            I believe the issue is that you need to pass the question variable from each loop in the parent view through to the partial using locals which allows the partial to access it.



                <%= render 'question_buttons', locals: { question: question } %>





            share|improve this answer























            • does this work on your version of rails? <%= render 'question_buttons', question: question %>?
              – vee
              Nov 9 at 0:50










            • i don't see the use of collection variable either. why do you have it there?
              – vee
              Nov 9 at 0:51















            up vote
            1
            down vote













            I believe the issue is that you need to pass the question variable from each loop in the parent view through to the partial using locals which allows the partial to access it.



                <%= render 'question_buttons', locals: { question: question } %>





            share|improve this answer























            • does this work on your version of rails? <%= render 'question_buttons', question: question %>?
              – vee
              Nov 9 at 0:50










            • i don't see the use of collection variable either. why do you have it there?
              – vee
              Nov 9 at 0:51













            up vote
            1
            down vote










            up vote
            1
            down vote









            I believe the issue is that you need to pass the question variable from each loop in the parent view through to the partial using locals which allows the partial to access it.



                <%= render 'question_buttons', locals: { question: question } %>





            share|improve this answer














            I believe the issue is that you need to pass the question variable from each loop in the parent view through to the partial using locals which allows the partial to access it.



                <%= render 'question_buttons', locals: { question: question } %>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 9:46









            Praveen Dhawan

            7718




            7718










            answered Nov 9 at 0:15









            Katrpilar

            113




            113












            • does this work on your version of rails? <%= render 'question_buttons', question: question %>?
              – vee
              Nov 9 at 0:50










            • i don't see the use of collection variable either. why do you have it there?
              – vee
              Nov 9 at 0:51


















            • does this work on your version of rails? <%= render 'question_buttons', question: question %>?
              – vee
              Nov 9 at 0:50










            • i don't see the use of collection variable either. why do you have it there?
              – vee
              Nov 9 at 0:51
















            does this work on your version of rails? <%= render 'question_buttons', question: question %>?
            – vee
            Nov 9 at 0:50




            does this work on your version of rails? <%= render 'question_buttons', question: question %>?
            – vee
            Nov 9 at 0:50












            i don't see the use of collection variable either. why do you have it there?
            – vee
            Nov 9 at 0:51




            i don't see the use of collection variable either. why do you have it there?
            – vee
            Nov 9 at 0:51












            up vote
            0
            down vote













            First:



            When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:



            <%= render 'question_buttons', collection: @accepted %>


            the partial question_buttons will be called for each item of @accepted array; that item will be available inside partial as question_buttons.



            If you want to use another name for the item, for example question, you need to call it as:



            <%= render 'question_buttons', collection: @accepted, as: :question %>


            Another option -- just rename the partial to question:



            <%= render 'question', collection: @accepted %>




            Second:



            In your code snippet rendering collection is called on each iteration of the loop over @accepted elements. If @accepted has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons partial needs to be rendered for each element of @accepted only once. In this case using collection param has no sense here. Just pass a local variable question into the partial:



            <%= render 'question_buttons', question: question %>





            share|improve this answer

























              up vote
              0
              down vote













              First:



              When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:



              <%= render 'question_buttons', collection: @accepted %>


              the partial question_buttons will be called for each item of @accepted array; that item will be available inside partial as question_buttons.



              If you want to use another name for the item, for example question, you need to call it as:



              <%= render 'question_buttons', collection: @accepted, as: :question %>


              Another option -- just rename the partial to question:



              <%= render 'question', collection: @accepted %>




              Second:



              In your code snippet rendering collection is called on each iteration of the loop over @accepted elements. If @accepted has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons partial needs to be rendered for each element of @accepted only once. In this case using collection param has no sense here. Just pass a local variable question into the partial:



              <%= render 'question_buttons', question: question %>





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                First:



                When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:



                <%= render 'question_buttons', collection: @accepted %>


                the partial question_buttons will be called for each item of @accepted array; that item will be available inside partial as question_buttons.



                If you want to use another name for the item, for example question, you need to call it as:



                <%= render 'question_buttons', collection: @accepted, as: :question %>


                Another option -- just rename the partial to question:



                <%= render 'question', collection: @accepted %>




                Second:



                In your code snippet rendering collection is called on each iteration of the loop over @accepted elements. If @accepted has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons partial needs to be rendered for each element of @accepted only once. In this case using collection param has no sense here. Just pass a local variable question into the partial:



                <%= render 'question_buttons', question: question %>





                share|improve this answer












                First:



                When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:



                <%= render 'question_buttons', collection: @accepted %>


                the partial question_buttons will be called for each item of @accepted array; that item will be available inside partial as question_buttons.



                If you want to use another name for the item, for example question, you need to call it as:



                <%= render 'question_buttons', collection: @accepted, as: :question %>


                Another option -- just rename the partial to question:



                <%= render 'question', collection: @accepted %>




                Second:



                In your code snippet rendering collection is called on each iteration of the loop over @accepted elements. If @accepted has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons partial needs to be rendered for each element of @accepted only once. In this case using collection param has no sense here. Just pass a local variable question into the partial:



                <%= render 'question_buttons', question: question %>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 5:39









                Ilya Konyukhov

                2,183618




                2,183618






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217982%2frendering-a-partial-inside-a-loop%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







                    Popular posts from this blog

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff