Rails3 acts_as_taggable with a form
I've just started working with the acts_as_taggable gem. Really liking it so far, but I am a bit unclear about how to use this gem with a form.
class Photo < ActiveRecord::Base
acts_as_taggable_on :tags
end
In my form for Photos I am trying to implement a series of checkboxes for the user to assign tags to their photo:
<%= f.label :tag_list %>
<%= f.check_box :tag_list, "landscape" %>
<%= f.check_box :tag_list, "people" %>
When viewing the form I get this error:
NoMethodError in Photos#edit
...line #19 raised:
undefined method `merge' for "landscape开发者_开发知识库":String
Extracted source (around line #19):
18: <div class="float_tag">
19: <%= f.check_box :tag_list, "landscape" %>
Any thoughts as to how I should create my form?
I'm assuming your <form>
looks something like this:
<%= form_for(@photo) do |f| %>
<%= f.label :tag_list %>
<%= f.check_box :tag_list, "landscape" %>
<%= f.check_box :tag_list, "people" %>
<% end %>
You should change up your f.checkbox
lines a bit:
<%= form_for(@photo) do |f| %>
<%= f.label :tag_list %>
<%= f.check_box :tag_list, { :multiple => true }, 'landscape', nil %>
<%= f.check_box :tag_list, { :multiple => true }, 'people', nil %>
<% end %>
Which will post something like this when submitted (with only people selected, for example):
{ :post => { :tag_list => ['', 'people'] } }
For anyone trying to get this to work with Rails 4 and strong parameters, I also had to permit the the tag_list param as an array.
params.require(:clip).permit(
:name, :other_params, { tag_list: [] }
)
精彩评论