【问题标题】:Rails Partial: Undefined Local VariableRails 部分:未定义的局部变量
【发布时间】:2016-01-10 14:47:19
【问题描述】:

我不经常使用rails,如果这很明显,请原谅。我已经查看了其他问题,正在 Ruby On Rail Guide 工作,看起来我做对了,但我仍然无法让我的局部变量工作。

todays/show.html.erb

<p>
  <strong>Tasks:</strong>  
  <%= render partial: "next_steps/next_step_today", 
        collection: @today_lines %>
</p>

next_steps/_next_step_today.html.erb

  <p>
    <%= render partial: "next_steps/next_step",
            locals: {next_step: today_line.next_step} %>
    <%= link_to 'x', today_line, method: :delete, data: { confirm: 'Are you sure?' } %>
  </p>

next_steps/_next_step.html.erb

<span class="<%= next_step.complete ? "completed_next_step" : ""%> next_step_span_<%= next_step.id%>">
<%= form_for(next_step, 
      :remote => true, 
      :html => {:'data-type' => 'json', 
                :multipart => true, 
                :id => "edit_next_step_#{next_step.id}_#{rand()}"}
      )  do |f| %>
    <%= f.hidden_field( :id, :value => next_step.id) %>
    <%= f.hidden_field( :note_id, :value => next_step.note_id) %>
    <%= f.hidden_field( :content, :value => next_step.content) %>
    <%= f.hidden_field( :due_date, :value => next_step.due_date) %>
    <%= f.check_box( :complete, :class => "next_step_complete_button" ) %>
    <%= next_step.content %>
<% end %>
</span>

我知道在我开始尝试添加部分以增加灵活性之前,最后一个部分 _next_step.html.erb 正在工作。现在我似乎无法正确传递当地人。

我之前的工作代码是

todays/show.html.erb

<p>
<strong>Tasks:</strong> 

<% @today_lines.each do |today_line| %>
  <p><%= render today_line.next_step %></p>
<% end %>
</p>

错误信息

今日名称错误#show

显示 /.../app/views/next_steps/_next_step_today.html.erb 其中第 3 行提出:

# 的未定义局部变量或方法 `today_line' 提取的源代码(第 3 行附近):

<p>
  <%= render partial: "next_steps/next_step",
          locals: {next_step: today_line.next_step} %>
  <%= link_to 'x', today_line, method: :delete, data: { confirm: 'Are you sure?' } %>
</p>

有什么想法吗?

编辑:最终工作代码

todays/show.html.erb

<p>
  <strong>Tasks:</strong>
  <%= render partial: "next_steps/next_step_today", 
    collection: @today_lines %>
</p>

_next_step_today.html.erb

  <p>
    <%= render "next_steps/next_step",
        next_step: next_step_today.next_step %>
    <%= link_to 'x', next_step_today, method: :delete, data: { confirm: 'Are you sure?' } %>
  </p>

collection: 确实要求您使用本地 var 的部分文件名。

我在搜索中还发现,如果你不使用partial:,你也可以省略locals:,而只使用变量名,这样更简洁。此更改显示在 _next_step_today.html.erb 中。我希望我也可以命名集合变量,而不是使用部分文件名,或者不是使用每个,但这已经足够接近并且可以工作了。

【问题讨论】:

    标签: ruby-on-rails ruby local-variables partials


    【解决方案1】:

    Show 需要指定部分将使用的键名。所以:

    <%= render partial: "next_steps/next_step_today", 
        today_lines: @today_lines %>
    

    修复 next_step_today 部分中的拼写:

      <%= render partial: "next_steps/next_step",
          locals: {next_step: today_lines.next_step} %>
    

    【讨论】:

      【解决方案2】:

      partial 中的变量名是从partial 本身的名称派生的,而不是从集合的名称派生的。

      您的 _next_step_today.html.erb 应如下所示:

      <p>
        <%= render partial: "next_steps/next_step",
              locals: {next_step: next_step_today.next_step} %>
        <%= link_to 'x', next_step_today, method: :delete, data: { confirm: 'Are you sure?' } %>
      </p>
      

      编辑:如果你想坚持@collection 约定,那就是

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-21
        • 2018-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-06
        • 2011-05-14
        相关资源
        最近更新 更多