如何在 Ruby on Rails 中使用一种表单更新两个模型?
我有两个模型,Page 和 PageContent。
class Page < ActiveRecord::Base
has_many :page_contents
end
class PageContent < ActiveRecord::Base
belongs_to :page
end
页面有一个 :css_design 属性,但我希望能够从我的 PageContent 表单编辑该属性。有什么想法吗?
我看到很多 accepts_nested_attributes 和 fields_for 建议,但它们不起作用,因为它们似乎都适用于 A 的表单. 从表单创建不同模型的全新实例(例如,从项目表单创建任务),或 B. 通过父级更新关联记录。 我想做相反的事情 - 我想通过关联记录更新家长的记录。
非常感谢任何帮助!非常感谢!
--Mark
UPDATE
我已将以下内容添加到我的 PageContent 模型中:
def css_design
page ? page.css_design : nil
end
def css_design= (val)
if page
page.update_attribute 'css_design', val
else
@css_design = val
end
end
after_create :set_page_css_design_on_create
def set_page_css_design_on_create
self.css_design = @css_design if page && @css_design
end
我在创建和更新操作中都添加了:
@page_content.update_attributes params[:page_content]
但我得到:
NoMethodError (undefined method `css_design=' for #<Page:0x00000003a80338>):
app/models/page_content.rb:13:in `css_design='
app/controllers/page_contents_controller.rb:56:in `new'
app/controllers/page_contents_controller.rb:56:in `create'
当我第一次创建 page_content 时。我直接从我的文件中复制并粘贴了这些内容,所以如果您发现任何奇怪的东西,请告诉我!
I have two models, Page and PageContent.
class Page < ActiveRecord::Base
has_many :page_contents
end
class PageContent < ActiveRecord::Base
belongs_to :page
end
Page has a :css_design attribute, but I want to be able to edit that attribute from my PageContent form. Any ideas?
I see a lot of accepts_nested_attributes and fields_for advice, but they don't work, because they all seem to be for forms that are either A. Creating entire new instances of a different model from a form (for example, creating tasks from a project form), or B. Updating associated records thru the parent. I want to do the opposite- I want to update the parent's record thru associated records.
Any help is greatly appreciated! Many thanks in advance!
--Mark
UPDATE
I have added the following to my PageContent model:
def css_design
page ? page.css_design : nil
end
def css_design= (val)
if page
page.update_attribute 'css_design', val
else
@css_design = val
end
end
after_create :set_page_css_design_on_create
def set_page_css_design_on_create
self.css_design = @css_design if page && @css_design
end
And I have, in both my create and update actions:
@page_content.update_attributes params[:page_content]
But I'm getting:
NoMethodError (undefined method `css_design=' for #<Page:0x00000003a80338>):
app/models/page_content.rb:13:in `css_design='
app/controllers/page_contents_controller.rb:56:in `new'
app/controllers/page_contents_controller.rb:56:in `create'
When I go to create the page_content for the first time. I copied and pasted this stuff straight from my files, so if you see anything weird, please let me know!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用以下内容更新您的模型:
您的表单现在可以显示并更新当前页面的值,即使它看起来只处理 PageContent:
在您的控制器中,例如:
Update your model with the following:
Your form can now display and update the current Page's value, even though it looks like it's only handling PageContent:
In your controller, e.g.:
如果字段不是太多,可以通过attr_accessor向PageContent模型添加属性。然后更新父级 after_save。像这样:
然后您可以为它放置一个表单字段,就像任何其他 PageContent 字段一样。如果有多个字段,请使用 update_attributes。另外,您可能想确保设置了属性(使用 attribute_present?(attribute)),这样它就不会用 nil 覆盖它。
(抱歉我没有时间立即测试。我稍后再尝试。)
If it's not too many fields, you can add attributes to the PageContent model via attr_accessor. Then update the parent after_save. Something like this:
Then you can put a form field for it just like any other PageContent field. If it's more than one field, use update_attributes. Also, you probably want to make sure the attribute is set (using attribute_present?(attribute)) so it doesn't overwrite it with nil.
(Sorry I don't have time to test it right away. I'll try later. )