Why does this locals variable declaration not work?

marcamillion

I have an initial partial call that looks like this:

 <% events_array.each_with_index do |event, index| %>
     <%= render partial: "events/activities/#{event.action}", locals: {event: event, index: index} %>
         <section class="card-overlay valign-wrapper">
               <div class="valign col s12 m6 l4 card-container">
                      <%= render partial: 'nodes/node', locals: {node: event.eventable.node, node_counter: index} %>
                </div>
          </section>
 <% end %>

That calls this nodes/node partial (truncated for brevity):

<div class="col s12 m6 l4 card-container">
  <div class="card" id="card-<%= node_counter %>">
    <!-- Card Content -->
    <div class="card-content" style="background-image: url('<%= node.media.try(:thumbnail_url) %>');">

Where I am now getting this error:

undefined local variable or method `node_counter' for #<#<Class:0x007f9067394f50>:0x007f9063b7d8b0>

At this line:

<div class="card" id="card-<%= node_counter %>">

Why does the locals option of my render partial:... not take care of this?

Note that the previous locals declaration of the other partial rendering works fine with no errors, so not sure why this doesn't work.

Edit 1

This is the full partial found at app/views/nodes/_node.html.erb

<div class="col s12 m6 l4 card-container">
  <div class="card" id="card-<%#= node_counter %>">
    <!-- Card Content -->
    <div class="card-content" style="background-image: url('<%= node.media.try(:thumbnail_url) %>');">

      <video id="video_<%= node_counter %>" class="card-video video-js vjs-default-skin" controls preload="none" data-setup="{}">
        <source src="<%= node.media.try(:zc_mp4_url) %>" type='video/mp4' />
        <p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
      </video>
      <a class="video-close"><i class="icon-close"></i></a>

      <div class="card-attr">
        <!-- Favorites -->
        <%= show_favorites_button(node, node_counter) %>
        <%= favorites_count(node) %>

        <!-- Comments -->
        <%= comments_count(node) %>
      </div>

      <!-- Tagged Users -->
      <div class="card-tagged-users">
        <%= render partial: "nodes/tagged_user", collection: node.tagged_users %>
        <span class="tagged-count"><%= node.cached_num_user_tags %></span>
      </div>

      <h3 class="card-title"><%= node.name %></h3>
    </div>
    <!-- End Tagged Users -->

    <!-- Card Meta -->
    <div class="card-meta">
      <aside>
        <%= image_tag node.user.avatar.url, class: "card-author-avatar", alt: "" %>
      </aside>
      <section>
        <h6 class="card-author-name"><%= node.user.name %></h6>
        <time class="card-date"><%= node.created_at.strftime("%B %d, %Y") %></time>
        <p class="card-desc"><%= node.media.description %>
      </section>
    </div>
    <!-- End Card Meta -->

    <!-- Card Comments -->
    <div class="card-comments-container">
      <h4 class="card-comments-title"><%= pluralize(node.comments_count, "Comment") %></h4>
      <a class="card-comments-expand"><i class="icon-arrow-down"></i></a>

      <div class="card-comments">
        <%= render partial: "nodes/comment", collection: node.comments.includes(:user).order(created_at: :desc) %>
      </div>
    </div>
    <!-- End Card Comments -->

    <!-- Card Input -->
    <div class="card-input">
      <%= simple_form_for([node, Comment.new], html: { id: "new_comment_card-#{node_counter}"}, remote: true) do |f| %>
       <%= f.error_notification %>
          <%= f.input_field :message, as: :text, id: "card-input-field-card-#{node_counter}", class: "input-field", placeholder: "Share your thoughts", cols: "30", rows: "10", "data-behavior" => "submit_on_enter" %>
          <%= f.button :submit, "Submit", name: "card_id", value: "card-#{node_counter}", id: "submit-card-#{node_counter}", class: "comment-submit", data: { disable_with: "Submitting Comment..." } %>
      <% end %>
    </div>
    <!-- End Card Input  -->
  <!-- End Card Content  -->

  </div>
</div>

Edit 2

So, inside Better Errors, at the REPL within the scope of the view that I call the partial on, i.e. the first code snippet in this question, I played around with the console and see that the local variables within the second partial are not being assigned at all, despite their values returning legitimate values:

>> node
!! #<NameError: undefined local variable or method `node' for #<#<Class:0x007fbf389e01c0>:0x007fbf30cd3358>>
>> event.eventable.node
=> #<Node id: 3, name: "Outro", family_tree_id: 1, user_id: 1, media_id: 3, media_type: "Video", created_at: "2015-07-25 04:28:39", updated_at: "2015-08-01 23:11:26", circa: nil, is_comment: nil, cached_votes_total: 0, cached_votes_score: 0, cached_votes_up: 0>
>> node_counter
!! #<NameError: undefined local variable or method `node_counter' for #<#<Class:0x007fbf389e01c0>:0x007fbf30cd3358>>
>> index
=> 0
>>

Why would the assignment not be happening?

Julie

The problem is that you are calling a partial named 'node' and so rails is expecting to define node and node_counter via other means. You can fix your code by doing the following:

<%= render partial: 'nodes/node', object: event.eventable.node, locals: { new_node_counter: index } %>

I believe you will need to use a different variable name for the node_counter. If you need to use the node_counter variable because other pages use this partial, you could always do something at the top of the partial 'nodes/node' like this:

<% node_counter = node_counter || new_node_counter %>

Here are some relevant excerpts from http://guides.rubyonrails.org/layouts_and_rendering.html:

Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the :object option:

<%= render partial: "customer", object: @new_customer %>

...

Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by _counter. For example, if you're rendering @products, within the partial you can refer to product_counter to tell you how many times the partial has been rendered. This does not work in conjunction with the as: :value option.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this locals variable declaration not work?

From Dev

Nameless variable declaration - why does it work?

From Dev

Why does the first one work and the second one not work? Global and Private variable declaration

From Dev

Why declaration of variable in closure environment did not work?

From Dev

How does a variable declaration work as a function?

From Dev

Why does forward declaration of struct not work?

From Dev

C function declaration syntax - why does this work?

From Dev

Retrieve locals() in order of variable declaration - python

From Dev

Why does this use of comma work in a expression but fail in a declaration?

From Dev

Warning: implicit declaration of function — why does my code work anyway?

From Dev

Forward Declaration of class as template param. Why does this work?

From Dev

WHY does this simple Hive table declaration work? As if by magic

From Dev

Why does JavaScript variable declaration at console results in "undefined" being printed?

From Dev

Why does empty variable declaration of type show compile error but not for null declaration?

From Dev

Why does variable expansion without $ work in expressions?

From Dev

Why string variable in JSON does not work?

From Dev

Why does changing a variable from a lambda not work?

From Dev

Why is a function declaration not a statement while a variable declaration is

From Dev

Javascript variable declaration, why is this legal?

From Dev

Why is variable declaration not allowed here?

From Dev

Why does printing a variable in global scope work, but modifying it does not?

From Dev

Why does printing a variable in global scope work, but modifying it does not?

From Dev

In C, if objects declared at block scope have no linkage, why does function declaration inside main() without "extern" work?

From Dev

Why does explicit method specialization of a template class work without its prototype declaration inside the class

From Dev

antlr4 grammar-locals clause doesn't generate variable declaration in Parser

From Dev

Why does the browser try to use an otherwise invalid property declaration when I introduce a CSS variable?

From Dev

Shell, bash, linux ; after variable declaration, why semi-colon and what does it do?

From Dev

Why does the compiler not complain when the struct keyword is reused in a function, with a variable declaration?

From Dev

Why does locals() return a strange self referential list?

Related Related

  1. 1

    Why does this locals variable declaration not work?

  2. 2

    Nameless variable declaration - why does it work?

  3. 3

    Why does the first one work and the second one not work? Global and Private variable declaration

  4. 4

    Why declaration of variable in closure environment did not work?

  5. 5

    How does a variable declaration work as a function?

  6. 6

    Why does forward declaration of struct not work?

  7. 7

    C function declaration syntax - why does this work?

  8. 8

    Retrieve locals() in order of variable declaration - python

  9. 9

    Why does this use of comma work in a expression but fail in a declaration?

  10. 10

    Warning: implicit declaration of function — why does my code work anyway?

  11. 11

    Forward Declaration of class as template param. Why does this work?

  12. 12

    WHY does this simple Hive table declaration work? As if by magic

  13. 13

    Why does JavaScript variable declaration at console results in "undefined" being printed?

  14. 14

    Why does empty variable declaration of type show compile error but not for null declaration?

  15. 15

    Why does variable expansion without $ work in expressions?

  16. 16

    Why string variable in JSON does not work?

  17. 17

    Why does changing a variable from a lambda not work?

  18. 18

    Why is a function declaration not a statement while a variable declaration is

  19. 19

    Javascript variable declaration, why is this legal?

  20. 20

    Why is variable declaration not allowed here?

  21. 21

    Why does printing a variable in global scope work, but modifying it does not?

  22. 22

    Why does printing a variable in global scope work, but modifying it does not?

  23. 23

    In C, if objects declared at block scope have no linkage, why does function declaration inside main() without "extern" work?

  24. 24

    Why does explicit method specialization of a template class work without its prototype declaration inside the class

  25. 25

    antlr4 grammar-locals clause doesn't generate variable declaration in Parser

  26. 26

    Why does the browser try to use an otherwise invalid property declaration when I introduce a CSS variable?

  27. 27

    Shell, bash, linux ; after variable declaration, why semi-colon and what does it do?

  28. 28

    Why does the compiler not complain when the struct keyword is reused in a function, with a variable declaration?

  29. 29

    Why does locals() return a strange self referential list?

HotTag

Archive