当使用accepts_nested_attributes_for时,如何编辑关联模型的属性?
我有三个模型:主题和文章由链接器连接。
class Topic < ActiveRecord::Base
has_many :linkers
has_many :articles, :through => :linkers, :foreign_key => :article_id
accepts_nested_attributes_for :articles
end
class Article < ActiveRecord::Base
has_many :linkers
has_many :topics, :through => :linkers, :foreign_key => :topic_id
end
class Linker < ActiveRecord::Base
#this is the join model, has extra attributes like "relevance"
belongs_to :topic
belongs_to :article
end
所以当我在主题控制器的“新”操作中构建文章时...
@topic.articles.build
...并使topics/new.html.erb中的嵌套表单...
<% form_for(@topic) do |topic_form| %>
...fields...
<% topic_form.fields_for :articles do |article_form| %>
...fields...
...Rails自动创建了链接器,这很棒。 现在我的问题是:我的链接器模型还有一些属性,我希望能通过“新主题”表单来更改这些属性。但是,Rails自动创建的链接器除了topic_id和article_id之外,所有属性都具有nil值。我该如何将那些其他链接器属性的字段放入“新主题”表单中,以便它们不为空?
Relationship
作为一个linker
,通过一个Account
来创建一个User
...但是我无法弄清楚这些新建和创建操作应该是什么样子的...你介意分享一下吗? - Mohamad