Rails3 acts_as_taggable with a form

人盡茶涼 提交于 2019-12-06 16:36:30

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: [] }
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!