Tin*_*n81 5 ruby ruby-on-rails ruby-on-rails-3
在我的Rails应用程序中Users
可以有许多People
反过来可以(但不必)属于Organisations
.
简而言之,这个:
Users --< People >-- Organisations
Run Code Online (Sandbox Code Playgroud)
现在,能够以某种方式在人员视图中创建新组织将是一件好事.它尝试了这个:
class Person < ActiveRecord::Base
attr_accessible :name, :organisation_attributes
belongs_to :user
belongs_to :organisation
accepts_nested_attributes_for :organisation
end
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为组织不是人的孩子.
还有另一种方法可以实现这一点吗?
谢谢你的帮助.
我可以看到它Person
实际上是一个孩子,Organisation
也可以为父模型制作嵌套表单.你已经在使用了accepts_nested_attributes_for
.
我假设您要显示Organisation
已保存的表单person
.然后
在您的PeopleController#show
方法中构建组织
@person.build_organisation
Run Code Online (Sandbox Code Playgroud)
并在 people/show.html.erb
form_for(@person) do |f|
f.fields_for(:organisation) do |fo|
# show the fields of organisation here.
end
end
Run Code Online (Sandbox Code Playgroud)
它应该工作.
更新:
我尝试了类似的东西,它的工作:)我已经做了一个包括片段的要点.请点击链接https://gist.github.com/3841507查看是否有效.
归档时间: |
|
查看次数: |
2106 次 |
最近记录: |