繁体   English   中英

Rails部分:未定义的局部变量

[英]Rails Partial: Undefined Local Variable

我很少使用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>

错误信息

Today#show中的NameError

显示/.../app/views/next_steps/_next_step_today.html.erb,其中#3行出现:

#<#:0x007f930adbd860>的未定义的局部变量或方法“ 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:确实要求您为局部变量使用部分文件名。

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

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

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

您的_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约定,那就是

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM