RoR 中的嵌套模型
我有以下模型:
class Topic < ActiveRecord::Base
has_many :posts, :dependent => :destroy
attr_accessible :name, :post_id
end
class Post < ActiveRecord::Base
belongs_to :topic, :touch => true
has_many :comments, :dependent => destroy
accepts_nested_attributes_for :topic, :comments
attr_accessible :name, :title, :content, :topic, :topic_attributes
end
class Comment < ActiveRecord::Base
belongs_to :Post
end
这个简单的表格有效吗?我可以同时访问 2 个嵌套模型吗?
simple_form_for @post do |f|
f.simple_fields_for :topic do |topic_form|
topic_form.input :name
end
f.simple_fields_for :comment do |comment_form|
comment_form.input :text
end
end
谢谢
I have the following Models:
class Topic < ActiveRecord::Base
has_many :posts, :dependent => :destroy
attr_accessible :name, :post_id
end
class Post < ActiveRecord::Base
belongs_to :topic, :touch => true
has_many :comments, :dependent => destroy
accepts_nested_attributes_for :topic, :comments
attr_accessible :name, :title, :content, :topic, :topic_attributes
end
class Comment < ActiveRecord::Base
belongs_to :Post
end
Is this simple form valid? Can I access 2 nested Models at the same time?
simple_form_for @post do |f|
f.simple_fields_for :topic do |topic_form|
topic_form.input :name
end
f.simple_fields_for :comment do |comment_form|
comment_form.input :text
end
end
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
Try this