你应该阅读rubyonrails API更深一点;)link
class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts
end
现在,您可以设置或通过属性哈希相关的岗位模型更新属性。
对于没有id键的每个散列,除非散列还包含一个计算结果为true的_destroy键,否则将新实例化一条新记录。
params = { :member => {
:name => 'joe', :posts_attributes => [
{ :title => 'Kari, the awesome Ruby documentation browser!' },
{ :title => 'The egalitarian assumption of the modern citizen' },
{ :title => '', :_destroy => '1' } # this will be ignored
]
}}
member = Member.create(params['member'])
member.posts.length # => 2
member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
来源
2013-02-08 18:57:48
FUT
我尝试,我只是不知道在哪里看。 :)很好知道属性散列,这是有道理的。谢谢! – jmcharnes 2013-02-08 19:09:37
是的。表单的实施取决于你。可能会选择宝石或您的个人实施。 – FUT 2013-02-08 19:12:08