Answer the question
In order to leave comments, you need to log in
Why does FormOptionsHelper collection_check_boxes (Rails 4.1.0) create an empty value (value="")?
There are two models Article and Tag associated with HABTM:
article.rb
class Article < ActiveRecord::Base
has_and_belongs_to_many :tags
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :articles
end
<p>
<%= f.label "Tags" %><br>
<%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name%>
<p>
<p>
<label for="article_Tags">Tags</label><br>
<input checked="checked" id="article_tag_ids_1" name="article[tag_ids][]" value="1" type="checkbox"><label for="article_tag_ids_1">Linux</label><input checked="checked" id="article_tag_ids_3" name="article[tag_ids][]" value="3" type="checkbox"><label for="article_tag_ids_3">Ubuntu</label><input checked="checked" id="article_tag_ids_4" name="article[tag_ids][]" value="4" type="checkbox"><label for="article_tag_ids_4">Windows</label><input name="article[tag_ids][]" value="" type="hidden">
</p>
"tag_ids"=>["3",
""]
Answer the question
In order to leave comments, you need to log in
The problem is in this line
<input name="article[tag_ids][]" value="" type="hidden">
generated by collection_check_boxes I suspect that, in the controller, there is not enough parameter for the white list - tag_ids: []params.require(:article).permit(...., tag_ids: [])
railscasts.com/episodes/196-nested-model-form-part-1 (outdated screencast, but gives an idea of the problem).
And the solution is here: api.rubyonrails.org/classes/ActiveRecord/NestedAtt...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question