具有多态 has_one 模型的嵌套属性

发布于 2024-08-31 09:08:34 字数 1723 浏览 8 评论 0原文

我在rails 2.3.5中使用accepts_nested_attributes_for和has_one多态模型 以下是模型及其关联:

class Address < ActiveRecord::Base
  attr_accessible :city, :address1, :address2
  belongs_to :addressable, :polymorphic => true
  validates_presence_of :address1, :address2, :city
end

class Vendor < ActiveRecord::Base
  attr_accessible :name, :address_attributes
  has_one  :address, :as => :addressable, :dependent => :destroy
  accepts_nested_attributes_for :address
end

这是视图:

- form_for @vendor do |f|
  = f.error_messages
  %p
    = f.label :name
    %br
    = f.text_field :name
  - f.fields_for :address_attributes do |address|
    = render "shared/address_fields", :f => address
  %p
    = f.submit "Create"

这是部分共享/address_fields.html.haml

%p
  = f.label :city
  %br= f.text_field :city
  %span City/Town name like Dharan, Butwal, Kathmandu, ..
%p
  = f.label :address1
  %br= f.text_field :address1
  %span City Street name like Lazimpat, New Road, ..
%p
  = f.label :address2
  %br= f.text_field :address2
  %span Tole, Marg, Chowk name like Pokhrel Tole, Shanti Marg, Pako, ..

这是控制器: 类 VendorsController < ApplicationController

  def new
    @vendor = Vendor.new
  end

  def create
    @vendor = Vendor.new(params[:vendor])
    if @vendor.save
      flash[:notice] = "Vendor created successfully!"
      redirect_to @vendor
    else
      render :action => 'new'
    end
  end
end

问题是当我填写所有字段时,记录会按预期保存在两个表中。

但是,当我只提交名称和城市或地址1时,验证有效,显示错误消息,但我在城市或地址1中输入的值没有保留或没有显示在地址表单字段中?

编辑操作也是如此。

虽然记录已保存,但地址不会显示在编辑表单上。仅显示客户端型号的名称。 实际上,当我查看日志时,根本没有查询到地址模型 SQL。

I am using accepts_nested_attributes_for with the has_one polymorphic model in rails 2.3.5
Following are the models and its associations:

class Address < ActiveRecord::Base
  attr_accessible :city, :address1, :address2
  belongs_to :addressable, :polymorphic => true
  validates_presence_of :address1, :address2, :city
end

class Vendor < ActiveRecord::Base
  attr_accessible :name, :address_attributes
  has_one  :address, :as => :addressable, :dependent => :destroy
  accepts_nested_attributes_for :address
end

This is the view:

- form_for @vendor do |f|
  = f.error_messages
  %p
    = f.label :name
    %br
    = f.text_field :name
  - f.fields_for :address_attributes do |address|
    = render "shared/address_fields", :f => address
  %p
    = f.submit "Create"

This is the partial shared/address_fields.html.haml

%p
  = f.label :city
  %br= f.text_field :city
  %span City/Town name like Dharan, Butwal, Kathmandu, ..
%p
  = f.label :address1
  %br= f.text_field :address1
  %span City Street name like Lazimpat, New Road, ..
%p
  = f.label :address2
  %br= f.text_field :address2
  %span Tole, Marg, Chowk name like Pokhrel Tole, Shanti Marg, Pako, ..

And this is the controller:
class VendorsController < ApplicationController

  def new
    @vendor = Vendor.new
  end

  def create
    @vendor = Vendor.new(params[:vendor])
    if @vendor.save
      flash[:notice] = "Vendor created successfully!"
      redirect_to @vendor
    else
      render :action => 'new'
    end
  end
end

The problem is when I fill in all the fileds, the record gets save on both tables as expected.

But when I just the name and city or address1 filed, the validation works, error message shown, but the value I put in the city or address1, is not persisted or not displayed inside the address form fields?

This is the same case with edit action too.

Though the record is saved, the address doesn't show up on the edit form. Only the name of the Client model is shown.
Actually, when I look at the log, the address model SQL is not queried even at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

幼儿园老大 2024-09-07 09:08:34

为什么是f.fields_for :address_attributes

不应该是这样吗:

- f.fields_for :address do |address_fields|
  = render "shared/address_fields", :f => address_fields

它不会在编辑和错误时加载值,因为您永远不会使用来自 @vendor.address 的值加载 address_attributes

Why f.fields_for :address_attributes?

Shouldn't it be:

- f.fields_for :address do |address_fields|
  = render "shared/address_fields", :f => address_fields

It's not loading the values on edit and errors because you never load address_attributes with the values from @vendor.address.

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