ruby - Unpermitted parameters for nested form many-to-many relationship Rails 4 -
i have simple blog app, want able create post , create new tag in same form, using nested form.
post , tag have many-to-many relationship, via join table:
class posttag < activerecord::base belongs_to :post belongs_to :tag end
here's tag model:
class tag < activerecord::base has_many :post_tags has_many :posts, :through => :post_tags validates_presence_of :name validates_uniqueness_of :name end
post model accepts nested attributes tags:
class post < activerecord::base has_many :post_tags has_many :tags, :through => :post_tags accepts_nested_attributes_for :tags validates_presence_of :name, :content end
on posts controller, permit tags_attributes:
def post_params params.require(:post).permit(:name, :content, :tag_ids => [], :tags_attributes => [:id, :name]) end
in form new post, want able either associate existing tags (via checkboxes) or create new 1 via nested form using fields_for:
.... <div class="field"> <%= f.collection_check_boxes :tag_ids, tag.all, :id, :name %><br> <%= f.fields_for [@post, tag.new] |tag_form| %> <p>add new tag:</p><br> <%= tag_form.label :name %> <%= tag_form.text_field :name %> <% end %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
my error "unpermitted parameters: tag":
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
change:
<%= f.fields_for [@post, tag.new] |tag_form| %>
to
<%= f.fields_for(:tags, tag.new) |tag_form| %>
Comments
Post a Comment