具有多态 has_one 模型的嵌套属性
我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么是
f.fields_for :address_attributes
?不应该是这样吗:
它不会在编辑和错误时加载值,因为您永远不会使用来自
@vendor.address
的值加载address_attributes
。Why
f.fields_for :address_attributes
?Shouldn't it be:
It's not loading the values on edit and errors because you never load
address_attributes
with the values from@vendor.address
.