我在使用 :collection 命令处理我在 rails 中创建的表单中的部分时遇到问题。理想情况下,我想使用 :collection 命令,这样我就可以在我的 .rjs 模板中轻松操作这一部分(当复选框更改时,表单将提交并重新加载表单,这是一个待办事项列表)。
此代码有效:
<% form_for "list[]", :url => {:action => "checkbox_update"} do |f| %>
<ul id="lists_not_completed">
<% for @list in @lists %>
<%= render :partial => @list, :locals => {:f =>f, :complete => FALSE } %>
<% end %>
</ul>
<% end %>
与部分:
<% if @list.completed == complete %>
<li><%= f.check_box :completed %>
<%=h @list.name %>
<%= link_to 'Show', list %>
<%= link_to 'Edit', edit_list_path(list) %>
<%= link_to 'Destroy', list, :confirm => 'Are you sure?', :method => :delete %></li>
<% end %>
此代码不起作用,但我希望它使用这种形式:
<% form_for "list[]", :url => {:action => "checkbox_update"} do |f| %>
<ul id="lists_not_completed">
<%= render :partial => 'list', :collection => @lists, :locals => {:f =>f, :complete => FALSE } %>
</ul>
<% end %>
与非工作部分:
<% if list.completed == complete %>
<li><%= f.check_box :completed %>
<%=h list.name %>
<%= link_to 'Show', list %>
<%= link_to 'Edit', edit_list_path(list) %>
<%= link_to 'Destroy', list, :confirm => 'Are you sure?', :method => :delete %></li>
<% end %>
我得到错误:
object[] 命名但 object param 和 @object var 不存在或不响应 to_param: nil。它指的是这一行:<li><%= f.check_box :completed %>
。我不确定为什么这不起作用并且尝试了很多很多不同的变体,但我无法让它工作。表格是否阻止我这样做?form_for 代码直接来自 Rails Way 手册,用于在表单中列出来自一个模型的多个对象。
对此的任何帮助将不胜感激。