【发布时间】:2011-01-12 01:13:11
【问题描述】:
在使用accepts_nested_attributes_for 时如何编辑连接模型的属性?
我有 3 个模型:链接者加入的主题和文章
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 值。如何将其他链接器属性的字段放入“新主题”表单中,以免它们出现为零?
【问题讨论】:
-
我正在尝试做和你一样的事情,只是在一个新的/创建动作中......我想知道你是否可以分享你的控制器动作。我想通过
Account使用Relationship作为linker创建一个User...但我无法弄清楚 new 和 create 操作的含义是什么...你介意吗?
标签: ruby-on-rails join nested nested-forms