将 form_for 与 Awesome Nested Set 一起使用

发布于 2024-12-10 08:29:26 字数 866 浏览 0 评论 0原文

我有一个启用了acts_as_nested_set的注释模型,但是当我尝试执行类似的操作(对于嵌套注释)时,我收到错误“未找到comment_comments_path”,大概是因为默认路径不适用于Awesome Nested Set。我该如何解决这个问题?

            <%= form_for([@comment, @comment.children.build]) do |f| %>

           <%= f.text_area :content, :placeholder=>'What do you think?'%>

           <%= f.submit 'Submit Reply'%>
            <%  end %>

我也尝试过这个:

            <%= form_for(@comment) do |f| %>

            <% @comment.children.each do |sub|  %>
            <%= f.fields_for :children, sub do |child| %>

            <%= child.text_area :content, :placeholder=>'What do you think?'%>

           <%= f.submit 'Submit Reply'%>
            <% end  %>
            <% end  %>
            <%  end %>

但它没有生成一个文本框供我输入。

I have a Comment model with the acts_as_nested_set enabled, but when I try to do something like this (for nested comments), i receive the error "comment_comments_path not found", presumably because the default pathing doesn't work with Awesome Nested Set. How do I get around this?

            <%= form_for([@comment, @comment.children.build]) do |f| %>

           <%= f.text_area :content, :placeholder=>'What do you think?'%>

           <%= f.submit 'Submit Reply'%>
            <%  end %>

I also tried this:

            <%= form_for(@comment) do |f| %>

            <% @comment.children.each do |sub|  %>
            <%= f.fields_for :children, sub do |child| %>

            <%= child.text_area :content, :placeholder=>'What do you think?'%>

           <%= f.submit 'Submit Reply'%>
            <% end  %>
            <% end  %>
            <%  end %>

but it didn't generate a textbox for me to type in.

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

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

发布评论

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

评论(1

吻泪 2024-12-17 08:29:26

你非常接近,是的,你必须先构建它,然后为其添加字段,所以:

<% @comment.children.build %>

<%= form_for([@comment]) do |f| %>
  <%= f.fields_for :children do |child| %>
    <%= child.text_area :content, :placeholder=>'What do you think?'%>
    <% end  %>
  <%= f.submit 'Submit Reply'%>
  <% end  %>
<% end  %>

这将为所有现有的子项+新的子项提供一个表单。如果您只想要一个新孩子的表格,那么您会需要这个:

<%= form_for([@comment]) do |f| %>
  <%= f.fields_for @comment.children.build, :children do |child| %>
    <%= child.text_area :content, :placeholder=>'What do you think?'%>
    <% end  %>
  <%= f.submit 'Submit Reply'%>
  <% end  %>
<% end  %>

You're very close, yeah you have to build it first then have fields for, so this:

<% @comment.children.build %>

<%= form_for([@comment]) do |f| %>
  <%= f.fields_for :children do |child| %>
    <%= child.text_area :content, :placeholder=>'What do you think?'%>
    <% end  %>
  <%= f.submit 'Submit Reply'%>
  <% end  %>
<% end  %>

This will have a form for all existing children + the new one. If you want only a form for a new child then you'll want this instead:

<%= form_for([@comment]) do |f| %>
  <%= f.fields_for @comment.children.build, :children do |child| %>
    <%= child.text_area :content, :placeholder=>'What do you think?'%>
    <% end  %>
  <%= f.submit 'Submit Reply'%>
  <% end  %>
<% end  %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文