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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With