Ruby on rails Rails部分:未定义的局部变量
我很少使用rails,所以如果这是显而易见的,请原谅。我已经看了其他的问题,我是从,看起来我做得很正确,但我仍然不能让我的局部变量工作 todays/show.html.erbRuby on rails Rails部分:未定义的局部变量,ruby-on-rails,ruby,local-variables,partials,Ruby On Rails,Ruby,Local Variables,Partials,我很少使用rails,所以如果这是显而易见的,请原谅。我已经看了其他的问题,我是从,看起来我做得很正确,但我仍然不能让我的局部变量工作 todays/show.html.erb <p> <strong>Tasks:</strong> <%= render partial: "next_steps/next_step_today", collection: @today_lines %> </p> &l
<p>
<strong>Tasks:</strong>
<%= render partial: "next_steps/next_step_today",
collection: @today_lines %>
</p>
<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>
<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>
<p>
<strong>Tasks:</strong>
<% @today_lines.each do |today_line| %>
<p><%= render today_line.next_step %></p>
<% end %>
</p>
<p>
<strong>Tasks:</strong>
<%= render partial: "next_steps/next_step_today",
collection: @today_lines %>
</p>
<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>
错误消息
《今日秀》中的名称错误
显示/../app/views/next_steps/_next_step_today.html.erb,其中第3行出现:
未定义的局部变量或方法“今日线”#
提取的源(第3行附近):
有什么想法吗
编辑:最终工作代码
todays/show.html.erb
<p>
<strong>Tasks:</strong>
<%= render partial: "next_steps/next_step_today",
collection: @today_lines %>
</p>
<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>
<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>
<p>
<strong>Tasks:</strong>
<% @today_lines.each do |today_line| %>
<p><%= render today_line.next_step %></p>
<% end %>
</p>
<p>
<strong>Tasks:</strong>
<%= render partial: "next_steps/next_step_today",
collection: @today_lines %>
</p>
<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>
任务:
\u下一步\u今天。html.erb
<p>
<strong>Tasks:</strong>
<%= render partial: "next_steps/next_step_today",
collection: @today_lines %>
</p>
<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>
<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>
<p>
<strong>Tasks:</strong>
<% @today_lines.each do |today_line| %>
<p><%= render today_line.next_step %></p>
<% end %>
</p>
<p>
<strong>Tasks:</strong>
<%= render partial: "next_steps/next_step_today",
collection: @today_lines %>
</p>
<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>
集合:
确实要求您为本地变量使用部分文件名
我在搜索中还发现,如果不使用
partial:
,也可以省略locals:
,而只使用变量名,这样更简洁。此更改显示在\u next\u step\u today.html.erb
中。我希望我也可以命名集合变量,而不是使用部分文件名,或者使用each,但这已经足够接近了,并且可以正常工作 Show需要指定分部将使用的密钥名。因此:
<%= render partial: "next_steps/next_step_today",
today_lines: @today_lines %>
修正下一步中的拼写,以及:
<%= render partial: "next_steps/next_step",
locals: {next_step: today_lines.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约定,那就是