我的保证人模型has_many地址为:addressable并接受地址的嵌套属性。但是,当将地址属性列入白名单时,params哈希不会识别addresses_attributes:[:street,:city等]。我一直在获取未经许可的参数地址。有没有人有这个问题?
我的模特:
class UserApplication < ActiveRecord::Base
belongs_to :user
has_many :guarantors, dependent: :destroy
accepts_nested_attributes_for :guarantors
end
class Guarantor < ActiveRecord::Base
has_many :addresses, :as => :addressable, dependent: :destroy
belongs_to :user_application, :foreign_key => :user_application_id
accepts_nested_attributes_for :addresses
end
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
来自UserApplications Controller 的
def user_application_params
params.require(:user_application).permit(...,
guarantors_attributes: [:id, :fname, :lname, :relationship, :phone, :email, :user_application_id,
addresses_attributes: [:id, :street, :street_2, :unit, :city, :state, :zip, :addressable_id, :addressable_type]]
)
end
&#39;嵌套&#39;形式的一部分:
<%= f.fields_for :guarantors do |builder| %>
<div class="row">
<div class="col-md-1">
<h6>FIRST NAME</h6>
<%= builder.text_field :fname, :class => "form-control" %>
</div>
<div class="col-md-2">
<h6>LAST NAME</h6>
<%= builder.text_field :lname, :class => "form-control" %>
</div>
<div class="col-md-2">
<h6>RELATIONSHIP</h6>
<%= builder.text_field :relationship, :class => "form-control" %>
</div>
<div class="col-md-2">
<h6>PHONE</h6>
<%= builder.number_field :phone, :class => "form-control" %>
</div>
<div class="col-md-2">
<h6>EMAIL</h6>
<%= builder.text_field :email, :class => "form-control" %>
</div>
</div>
<%= f.fields_for :addresses do |builder| %>
<div class="col-md-1">
<h6>ADDRESS</h6>
<%= builder.text_field :street, :class => "form-control" %>
</div>
<div class="col-md-2">
<h6>ADDRESS 2</h6>
<%= builder.text_field :street_2, :class => "form-control" %>
</div>
<div class="col-md-2">
<h6>CITY</h6>
<%= builder.text_field :city, :class => "form-control" %>
</div>
<div class="col-md-2">
<h6>STATE</h6>
<%= builder.text_field :state, :class => "form-control" %>
</div>
<div class="col-md-2">
<h6>ZIP</h6>
<%= builder.number_field :zip, :class => "form-control" %>
</div>
</div>
<% end %>
<% end %>
最后,参数:
"guarantors_attributes"=>{"0"=>{"fname"=>"blah", "lname"=>"", "relationship"=>"", "phone"=>"", "email"=>"", "id"=>"68"}, "1"=>{"fname"=>"", "lname"=>"", "relationship"=>"", "phone"=>"", "email"=>"", "id"=>"69"}}, "addresses"=>{"street"=>"", "street_2"=>"", "city"=>"", "state"=>"", "zip"=>""}, ...
我不断获取未经许可的参数地址,并且它也没有嵌套在guarantors_attributes中。有任何想法吗?提前谢谢。
答案 0 :(得分:0)
当您构建fields_for :addresses
时,我认为您希望它是builder.fields_for
,因为地址嵌套在担保人之下。
f.fields_for :guarantors do |builder|
# fields
builder.fields_for :addresses do |builder|
# fields
end
end
我还会使块名更具体,因为有2 builder
个变量让您感到困惑。
f.fields_for :guarantors do |guarantor_form|
# fields
guarantor_form.fields_for :addresses do |address_form|
# fields
end
end