Ruby 嵌套表单多对多关系Rails 4的不允许参数
我有一个简单的博客应用程序,我希望能够创建一篇文章,并使用嵌套表单以相同的形式为其创建一个新标记 Post和Tag通过联接表具有多对多关系:Ruby 嵌套表单多对多关系Rails 4的不允许参数,ruby,ruby-on-rails-4,many-to-many,nested-forms,nested-attributes,Ruby,Ruby On Rails 4,Many To Many,Nested Forms,Nested Attributes,我有一个简单的博客应用程序,我希望能够创建一篇文章,并使用嵌套表单以相同的形式为其创建一个新标记 Post和Tag通过联接表具有多对多关系: class PostTag < ActiveRecord::Base belongs_to :post belongs_to :tag end 在我的新帖子表单中,我希望能够通过复选框关联现有的标签,或者通过嵌套表单使用以下字段创建新标签: .... <div class="field"> <%= f.colle
class PostTag < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
在我的新帖子表单中,我希望能够通过复选框关联现有的标签,或者通过嵌套表单使用以下字段创建新标签:
....
<div class="field">
<%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name %><br>
<%= f.fields_for [@post, Tag.new] do |tag_form| %>
<p>Add a new tag:</p><br>
<%= tag_form.label :name %>
<%= tag_form.text_field :name %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
更改:
<%= f.fields_for [@post, Tag.new] do |tag_form| %>
到
更改:
<%= f.fields_for [@post, Tag.new] do |tag_form| %>
到
就这样,谢谢,@Broisasze!你介意指导我找一些资源/文档来解释原因吗?再次感谢。@ahimmelstoss-最好的文档永远是源代码,没有任何文档可以告诉你幕后的全部故事。这里有两种方法可以看:和。就是这样,谢谢,@broisasse!你介意指导我找一些资源/文档来解释原因吗?再次感谢。@ahimmelstoss-最好的文档永远是源代码,没有任何文档可以告诉你幕后的全部故事。以下是两种方法:和。
....
<div class="field">
<%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name %><br>
<%= f.fields_for [@post, Tag.new] do |tag_form| %>
<p>Add a new tag:</p><br>
<%= tag_form.label :name %>
<%= tag_form.text_field :name %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Parameters: {"utf8"=>"✓", "authenticity_token"=>"dZnCgFxrvuoY4bIUMMxI7kTLEr/R32pUX55wwHZsS4Q=", "post"=>{"name"=>"post title", "content"=>"post content", "tag_ids"=>[""], "tag"=>{"name"=>"new tag"}}, "commit"=>"Create Post"}
Unpermitted parameters: tag
<%= f.fields_for [@post, Tag.new] do |tag_form| %>
<%= f.fields_for(:tags, Tag.new) do |tag_form| %>