[英]Rails 4 Nested form has_many through association and database
So I have the three models client, address and address_info. 所以我有三个模型客户端,地址和地址信息。 When I create a nested form for address and address_info however I get basically double the rows that I meant to give it. 但是,当我为address和address_info创建一个嵌套表单时,我得到的行基本上是我想要的两倍。 The first three rows will have the addresstype and clientid tied to it and the other three will have clientid and addressid tied to it. 前三行将具有绑定的addresstype和clientid,其他三行将具有与其绑定的clientid和addressid。 Here is an example of the output: 这是输出示例:
As you can see, the rows do not have both the address and the client id's associated with the row. 如您所见,这些行没有与该行关联的地址和客户端ID。 I'm not sure how to fix that. 我不确定该如何解决。
Here are my models: 这是我的模型:
#client model
has_many :address_infos
has_many :addresses, through: :address_infos
accepts_nested_attributes_for :addresses, :address_infos, reject_if: :all_blank, allow_destroy: true
#addressinfo model
belongs_to :client
belongs_to :address
#address model
has_many :address_infos
has_many :clients, through: :address_infos
Here is my clients controller: 这是我的客户控制器:
def new
@client = Client.new
@client.address_infos.build
@client.addresses.build
end
def create
@client = Client.new(client_params)
end
private
def client_params
params.require(:client).permit(:firstname, :middlename, :lastname,
addresses_attributes: [:street, :city, :state, :zipcode],
address_infos_attributes: [:ownorrent, :addresstype, :yearsofresidency])
end
end
Here is my view, note that there are also nested forms for a mailing and former address that I did not include: 这是我的观点,请注意,对于邮件和以前的地址,我也没有嵌套的表格:
<%= form_for(@client) do |f| %>
<%= f.fields_for :addresses do |addresses_form| %>
<!-- misc. code -->
<%= f.fields_for :address_infos do |addressinfo_form| %>
<%= addressinfo_form.label :ownorrent, "Own", :value => "Own" %>
<%= addressinfo_form.radio_button :ownorrent, :own %>
<%= addressinfo_form.label :ownorrent, "Rent", :value => "Rent" %>
<%= addressinfo_form.radio_button :ownorrent, :rent %>
<%= addressinfo_form.text_field :yearsofresidency, class:"form-control", id:"inputfield", placeholder:"Years" %>
<%= addressinfo_form.text_field :addresstype,:value => "presentaddress" %>
<% end %>
<% end %>
UPDATE So after doing what was suggested, I end up with this in my database: The address I tried to add was for 123 Fake St. The other things were seeded directly. 更新因此,在执行了建议的操作之后,我最终在数据库中添加了该地址:我尝试添加的地址是123 Fake St的地址。其他内容直接植入了种子。 The address_id still does not stick with the rest of the stuff. address_id仍然与其余内容不符。 I also updated my current models. 我还更新了当前模型。
You should control nested attributes before saving in DB. 在保存到数据库之前,应该控制嵌套属性。 Use reject_if
or allow_blank
options to prevent and destroy blank records. 使用reject_if
或allow_blank
选项可防止和销毁空白记录。
accepts_nested_attributes_for :addresses, :address_infos, reject_if: :all_blank, allow_destroy: true
For more options to control the params, visits the link http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html 有关控制参数的更多选项,请访问链接http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.