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

发布于 2024-11-06 07:48:46 字数 1417 浏览 2 评论 0原文

我有两个模型,Page 和 PageContent。

class Page < ActiveRecord::Base
  has_many :page_contents
end

class PageContent < ActiveRecord::Base
  belongs_to :page
end

页面有一个 :css_design 属性,但我希望能够从我的 PageContent 表单编辑该属性。有什么想法吗?

我看到很多 accepts_nested_attributesfields_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-11-13 07:48:46

使用以下内容更新您的模型:

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

您的表单现在可以显示并更新当前页面的值,即使它看起来只处理 PageContent:

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

在您的控制器中,例如:

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

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:

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

In your controller, e.g.:

def update
  ...
  @page_content.update_attributes params[:page_content]
  ...
end
浅浅淡淡 2024-11-13 07:48:46

如果字段不是太多,可以通过attr_accessor向PageContent模型添加属性。然后更新父级 after_save。像这样:

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

然后您可以为它放置一个表单字段,就像任何其他 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:

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. 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. )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文