ruby-on-rails – 为什么这个本地变量声明不起作用?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 为什么这个本地变量声明不起作用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个初始的部分通话,如下所示:
<% 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 %>

这称为节点/节点部分(为了简洁而被截短):

<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) %>');">

我现在得到这个错误

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

在这一行:

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

为什么我的渲染部分的本地选项:…不关心这个?

请注意,其他部分渲染的以前的本地声明工作正常,没有错误,因此不知道为什么这不起作用.

编辑1

这是在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>

编辑2

所以,在更好的错误里面,在我称之为部分的视图范围内的REPL,即这个问题中的第一个代码片段,我在控制台上玩过,看到第二个部分的局部变量不是尽管他们的价值观返回合法价值:

>> 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
>>

为什么这项任务不会发生?

解决方法

问题是你正在调用一个名为“node”的部分,所以rails希望通过其他方式定义node和node_counter.您可以通过执行以下操作来修复代码

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

我相信你将需要为node_counter使用不同的变量名.如果您需要使用node_counter变量,因为其他页面使用此部分,您可以随时在部分“nodes / node”的顶部执行以下操作:

<%node_counter = node_counter || new_node_counter%>

以下是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.

原文链接:https://www.f2er.com/ruby/271594.html

猜你在找的Ruby相关文章