Is it possible to render a partial by passing a local variable for the collection?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















In Rails 5.2.2 I am trying to render a partial using a local variable as the collection, but when running a test to get the view I get an error in the partial:



ActionView::Template::Error:         ActionView::Template::Error: undefined local variable or method `item' for #<#<Class:0x00000007503de8>:0x000000093f2240>
app/views/work_centers/_item_kanban.html.erb:1:in `_app_views_work_centers__item_kanban_html_erb__413475955005549970_77587680'
app/views/work_centers/kanban.html.erb:5:in `block in _app_views_work_centers_kanban_html_erb__2029427675221229249_77567000'
app/views/work_centers/kanban.html.erb:4:in `each'
app/views/work_centers/kanban.html.erb:4:in `_app_views_work_centers_kanban_html_erb__2029427675221229249_77567000'
test/controllers/work_centers_controller_test.rb:27:in `block in <class:WorkCentersControllerTest>'


work_centers_controller.rb



def kanban
# Get hash of form {"Laser"=>["Item1", "Item2"], "Brake"=>["Item2"]}
@wc_items = HashWithIndifferentAccess.new
WorkCenter.all.each do |wc|
@wc_items[wc.name] = Item.where('id IN (SELECT item_id
FROM routings
WHERE work_center_id = ?)', wc.id)
.where('id NOT IN (SELECT item_id
FROM status_updates
WHERE work_center_id = ?)', wc.id)
.pluck(:number)
end
end


kanban.html.erb



<% @wc_items.each do |wc, items| %>
<%= render partial: 'item_kanban', collection: items %>
<% end %>


_item_kanban.html.erb



<%= link_to item, class: "list-group-item" do %>
<%= item.number %>
<% end %>


In other views I render a partial using a collection instance variable (e.g. @items) and reference the singular variable in the partial (e.g. item). I expected this partial to behave the same way using a local collection variable.










share|improve this question





























    1















    In Rails 5.2.2 I am trying to render a partial using a local variable as the collection, but when running a test to get the view I get an error in the partial:



    ActionView::Template::Error:         ActionView::Template::Error: undefined local variable or method `item' for #<#<Class:0x00000007503de8>:0x000000093f2240>
    app/views/work_centers/_item_kanban.html.erb:1:in `_app_views_work_centers__item_kanban_html_erb__413475955005549970_77587680'
    app/views/work_centers/kanban.html.erb:5:in `block in _app_views_work_centers_kanban_html_erb__2029427675221229249_77567000'
    app/views/work_centers/kanban.html.erb:4:in `each'
    app/views/work_centers/kanban.html.erb:4:in `_app_views_work_centers_kanban_html_erb__2029427675221229249_77567000'
    test/controllers/work_centers_controller_test.rb:27:in `block in <class:WorkCentersControllerTest>'


    work_centers_controller.rb



    def kanban
    # Get hash of form {"Laser"=>["Item1", "Item2"], "Brake"=>["Item2"]}
    @wc_items = HashWithIndifferentAccess.new
    WorkCenter.all.each do |wc|
    @wc_items[wc.name] = Item.where('id IN (SELECT item_id
    FROM routings
    WHERE work_center_id = ?)', wc.id)
    .where('id NOT IN (SELECT item_id
    FROM status_updates
    WHERE work_center_id = ?)', wc.id)
    .pluck(:number)
    end
    end


    kanban.html.erb



    <% @wc_items.each do |wc, items| %>
    <%= render partial: 'item_kanban', collection: items %>
    <% end %>


    _item_kanban.html.erb



    <%= link_to item, class: "list-group-item" do %>
    <%= item.number %>
    <% end %>


    In other views I render a partial using a collection instance variable (e.g. @items) and reference the singular variable in the partial (e.g. item). I expected this partial to behave the same way using a local collection variable.










    share|improve this question

























      1












      1








      1








      In Rails 5.2.2 I am trying to render a partial using a local variable as the collection, but when running a test to get the view I get an error in the partial:



      ActionView::Template::Error:         ActionView::Template::Error: undefined local variable or method `item' for #<#<Class:0x00000007503de8>:0x000000093f2240>
      app/views/work_centers/_item_kanban.html.erb:1:in `_app_views_work_centers__item_kanban_html_erb__413475955005549970_77587680'
      app/views/work_centers/kanban.html.erb:5:in `block in _app_views_work_centers_kanban_html_erb__2029427675221229249_77567000'
      app/views/work_centers/kanban.html.erb:4:in `each'
      app/views/work_centers/kanban.html.erb:4:in `_app_views_work_centers_kanban_html_erb__2029427675221229249_77567000'
      test/controllers/work_centers_controller_test.rb:27:in `block in <class:WorkCentersControllerTest>'


      work_centers_controller.rb



      def kanban
      # Get hash of form {"Laser"=>["Item1", "Item2"], "Brake"=>["Item2"]}
      @wc_items = HashWithIndifferentAccess.new
      WorkCenter.all.each do |wc|
      @wc_items[wc.name] = Item.where('id IN (SELECT item_id
      FROM routings
      WHERE work_center_id = ?)', wc.id)
      .where('id NOT IN (SELECT item_id
      FROM status_updates
      WHERE work_center_id = ?)', wc.id)
      .pluck(:number)
      end
      end


      kanban.html.erb



      <% @wc_items.each do |wc, items| %>
      <%= render partial: 'item_kanban', collection: items %>
      <% end %>


      _item_kanban.html.erb



      <%= link_to item, class: "list-group-item" do %>
      <%= item.number %>
      <% end %>


      In other views I render a partial using a collection instance variable (e.g. @items) and reference the singular variable in the partial (e.g. item). I expected this partial to behave the same way using a local collection variable.










      share|improve this question














      In Rails 5.2.2 I am trying to render a partial using a local variable as the collection, but when running a test to get the view I get an error in the partial:



      ActionView::Template::Error:         ActionView::Template::Error: undefined local variable or method `item' for #<#<Class:0x00000007503de8>:0x000000093f2240>
      app/views/work_centers/_item_kanban.html.erb:1:in `_app_views_work_centers__item_kanban_html_erb__413475955005549970_77587680'
      app/views/work_centers/kanban.html.erb:5:in `block in _app_views_work_centers_kanban_html_erb__2029427675221229249_77567000'
      app/views/work_centers/kanban.html.erb:4:in `each'
      app/views/work_centers/kanban.html.erb:4:in `_app_views_work_centers_kanban_html_erb__2029427675221229249_77567000'
      test/controllers/work_centers_controller_test.rb:27:in `block in <class:WorkCentersControllerTest>'


      work_centers_controller.rb



      def kanban
      # Get hash of form {"Laser"=>["Item1", "Item2"], "Brake"=>["Item2"]}
      @wc_items = HashWithIndifferentAccess.new
      WorkCenter.all.each do |wc|
      @wc_items[wc.name] = Item.where('id IN (SELECT item_id
      FROM routings
      WHERE work_center_id = ?)', wc.id)
      .where('id NOT IN (SELECT item_id
      FROM status_updates
      WHERE work_center_id = ?)', wc.id)
      .pluck(:number)
      end
      end


      kanban.html.erb



      <% @wc_items.each do |wc, items| %>
      <%= render partial: 'item_kanban', collection: items %>
      <% end %>


      _item_kanban.html.erb



      <%= link_to item, class: "list-group-item" do %>
      <%= item.number %>
      <% end %>


      In other views I render a partial using a collection instance variable (e.g. @items) and reference the singular variable in the partial (e.g. item). I expected this partial to behave the same way using a local collection variable.







      ruby-on-rails






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 16:45









      RyanRyan

      111112




      111112
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Please try it



          <%= render partial: 'item_kanban', collection: items, as: :item %>


          Now you can access



          <%= link_to item, class: "list-group-item" do %>
          <%= item.number %>
          <% end %>


          When you did



          <%= render partial: 'item_kanban', collection: items %> will give you a item_kanban local variable and not a item variable.



          So you need to specify it by as: :item now you can access item






          share|improve this answer
























          • That fixed it, thank you!

            – Ryan
            Jan 3 at 17:21












          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%2f54026449%2fis-it-possible-to-render-a-partial-by-passing-a-local-variable-for-the-collectio%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









          0














          Please try it



          <%= render partial: 'item_kanban', collection: items, as: :item %>


          Now you can access



          <%= link_to item, class: "list-group-item" do %>
          <%= item.number %>
          <% end %>


          When you did



          <%= render partial: 'item_kanban', collection: items %> will give you a item_kanban local variable and not a item variable.



          So you need to specify it by as: :item now you can access item






          share|improve this answer
























          • That fixed it, thank you!

            – Ryan
            Jan 3 at 17:21
















          0














          Please try it



          <%= render partial: 'item_kanban', collection: items, as: :item %>


          Now you can access



          <%= link_to item, class: "list-group-item" do %>
          <%= item.number %>
          <% end %>


          When you did



          <%= render partial: 'item_kanban', collection: items %> will give you a item_kanban local variable and not a item variable.



          So you need to specify it by as: :item now you can access item






          share|improve this answer
























          • That fixed it, thank you!

            – Ryan
            Jan 3 at 17:21














          0












          0








          0







          Please try it



          <%= render partial: 'item_kanban', collection: items, as: :item %>


          Now you can access



          <%= link_to item, class: "list-group-item" do %>
          <%= item.number %>
          <% end %>


          When you did



          <%= render partial: 'item_kanban', collection: items %> will give you a item_kanban local variable and not a item variable.



          So you need to specify it by as: :item now you can access item






          share|improve this answer













          Please try it



          <%= render partial: 'item_kanban', collection: items, as: :item %>


          Now you can access



          <%= link_to item, class: "list-group-item" do %>
          <%= item.number %>
          <% end %>


          When you did



          <%= render partial: 'item_kanban', collection: items %> will give you a item_kanban local variable and not a item variable.



          So you need to specify it by as: :item now you can access item







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 17:02









          Rajarshi DasRajarshi Das

          11.2k33251




          11.2k33251













          • That fixed it, thank you!

            – Ryan
            Jan 3 at 17:21



















          • That fixed it, thank you!

            – Ryan
            Jan 3 at 17:21

















          That fixed it, thank you!

          – Ryan
          Jan 3 at 17:21





          That fixed it, thank you!

          – Ryan
          Jan 3 at 17:21




















          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%2f54026449%2fis-it-possible-to-render-a-partial-by-passing-a-local-variable-for-the-collectio%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith