Rails3 actions_as_taggable 带有表单

发布于 2024-12-08 14:56:38 字数 665 浏览 0 评论 0原文

我刚刚开始使用acts_as_taggable gem。到目前为止我真的很喜欢它,但我有点不清楚如何将这个 gem 与表单一起使用。

class Photo < ActiveRecord::Base
  acts_as_taggable_on :tags
end

在我的照片表单中,我试图实现一系列复选框,以便用户为他们的照片分配标签:

<%= f.label :tag_list %>
<%= f.check_box :tag_list, "landscape" %>
<%= f.check_box :tag_list, "people" %>

查看表单时,我收到此错误:

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" %>

关于如何创建表单有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思故 2024-12-15 14:56:38

我假设您的

看起来像这样:

<%= form_for(@photo) do |f| %>
  <%= f.label :tag_list %>
  <%= f.check_box :tag_list, "landscape" %>
  <%= f.check_box :tag_list, "people" %>
<% end %>

您应该稍微更改一下 f.checkbox 行:

<%= 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 %>

提交时会发布类似的内容(使用仅选择人员,例如):

{ :post => { :tag_list => ['', 'people'] } }

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'] } }
墨离汐 2024-12-15 14:56:38

对于任何试图让它与 Rails 4 和强参数一起工作的人,我还必须允许 tag_list 参数作为数组。

params.require(:clip).permit(
  :name, :other_params, { tag_list: [] }
)

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: [] }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文