当我有如下关系时:
class Programmer < ActiveRecord::Base
has_many :projects
has_many :assignments, :through => :projects
acts_as_taggable
end
class Assignment < ActiveRecord::Base
has_many :projects
has_many :programmers, :through => :projects
end
以单一形式输入程序员和作业数据要求我有一个嵌套的表格。为什么当我有一个标签字段时,标签不需要是嵌套的形式?
例如,这不起作用
<%= form_for(@programmer) do |p| %>
<%= p.label :name %><br />
<%= p.text_field :name %><br />
<%= p.label :assignments %><br />
<%= p.text_field :assignments %><br />
<%= p.submit %>
<% end %>
但为什么会这样呢?
<%= form_for(@programmer) do |p| %>
<%= p.label :name %><br />
<%= p.text_field :name %><br />
<%= p.label :tag_list %><br />
<%= p.text_field :tag_list %><br />
<%= p.submit %>
<% end %>
答案 0 :(得分:0)
不太详细的答案是当您使用tag_list=
时,acts_as_taggable_on处理标记创建。
示例IRB会话:
$> photo = Photo.last
$> photo.tag_list # ['outer space', 'andromeda']
$> photo.tag_list = "outer space, andromeda, galaxy"
$> photo.tag_list # ['outer space', 'andromeda', 'galaxy']
$> photo.save
# checks the db for existing outer space, andromeda, and
# galaxy tags on the photo, creates any missing ones,
# deletes any ones it previously had but you didn't specify
acts_as_taggable_on可以在此方法中合理地实现此行为,因为库可以假设您要检查重复项,并删除您未提供的任何内容。默认情况下,在任何通用关联上执行此操作都会很糟糕:)