Ruby on Rails - 部分使用集合循环时出现问题

发布于 2024-07-08 10:40:19 字数 1963 浏览 4 评论 0原文

我在使用 :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。 它指的是这一行:

  • <%= f.check_box :completed %>。 我不确定为什么这不起作用,并且尝试了很多很多不同的变体,但我无法让它工作。 该表格是否阻止我这样做? form_for 代码直接来自 Rails Way 书籍,用于在表单中列出一个模型中的多个对象。
  • 对此的任何帮助将不胜感激。

    I am having issues using the :collection command for a partial within a form I am creating in rails. I would ideally like to use the :collection command, so I can easily manipulate this section in my .rjs templates (the form will submit and reload the form when the check box is changed, it's a to-do list).

    This code works:

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

    with the partial:

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

    This code does not work, but I would like it to use this form:

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

    with the non-working partial:

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

    I get the error:

    object[] naming but object param and @object var don't exist or don't respond to to_param: nil. It is referring to this line: <li><%= f.check_box :completed %>. I'm not sure if why this doesn't work and have tried many, many different variations, but I can't get it working. Is the form preventing me from doing this? The form_for code is straight from the Rails Way book for listing multiple objects from one model in a form.

    Any help on this would be greatly appreciated.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    年少掌心 2024-07-15 10:40:19

    我认为问题是当您使用 render :partial 和 :collection 时,您没有在任何地方定义 @list 。

    当您调用 f.check_box 时,系统正在寻找 @list 来匹配 list[] ,

    您可以在部分中设置 @list = list 来解决这个问题。 我想。

    I think that the problem is you've not got @list defined anywhere when you're using the render :partial with a :collection.

    The system is looking for @list to match the list[] when you call f.check_box

    you could set @list = list in your partial to get around that. I suppose.

    不甘平庸 2024-07-15 10:40:19

    蒂姆的答案是正确的,但我可能会避免完全提取 form_for 循环中的部分内容。 我认为这是一个风格问题,但我认为这里的混乱并不值得在这种情况下进行部分代表的清理。 我可能会写一个包含整个表格的部分内容。

    Tim's answer is correct, but I'd probably avoid extracting the partial within the form_for loop altogether. I suppose it's a matter of style, but I think the confusion here isn't really worth the cleanup that the partial represents in this case. I'd probably write a partial that included the whole form.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文