简体   繁体   English

如何在Ruby on Rails中使用一个表单更新两个模型?

[英]How do I update two models with one form in Ruby on Rails?

I have two models, Page and PageContent. 我有两个模型,Page和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. 页面有一个:css_design属性,但我希望能够从我的PageContent表单中编辑该属性。 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. 我看到很多accepts_nested_attributesfields_for建议,但是它们不起作用 ,因为它们似乎都是用于A的表单。从表单创建不同模型的全新实例(例如,从项目创建任务)形式),或B.通过父级更新相关记录。 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 UPDATE


I have added the following to my PageContent model: 我已将以下内容添加到我的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

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. 当我第一次创建page_content时。 I copied and pasted this stuff straight from my files, so if you see anything weird, please let me know! 我直接从我的文件中复制并粘贴了这些东西,所以如果你看到任何奇怪的东西,请告诉我!

If it's not too many fields, you can add attributes to the PageContent model via attr_accessor. 如果字段不是太多,您可以通过attr_accessor向PageContent模型添加属性。 Then update the parent after_save. 然后更新父After_save。 Something like this: 像这样的东西:

class PageContent < ActiveRecord::Base
  belongs_to :page

  attr_accessor :css_design

  after_save :update_parent_css

  private

    def update_parent_css
      self.page.update_attribute(:css_design, self.css_design)
    end

end

Then you can put a form field for it just like any other PageContent field. 然后,您可以为其添加表单字段,就像任何其他PageContent字段一样。 If it's more than one field, use update_attributes. 如果它不止一个字段,请使用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. 此外,您可能希望确保设置属性(使用attribute_present?(attribute)),因此它不会用nil覆盖它。

(Sorry I don't have time to test it right away. I'll try later. ) (对不起,我没时间马上测试一下。我会稍后再试。)

Update your model with the following: 使用以下内容更新您的模型:

class PageContent < ActiveRecord::Base
  belongs_to :page
  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
end

Your form can now display and update the current Page's value, even though it looks like it's only handling PageContent: 您的表单现在可以显示和更新当前页面的值,即使它看起来只是处理PageContent:

<%= form_for @page_content do |f| %>
  ...
  <%= f.text_field :css_design %>
  ...
<% end %>

In your controller, eg: 在您的控制器中,例如:

def update
  ...
  @page_content.update_attributes params[:page_content]
  ...
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM