【发布时间】:2014-09-26 16:36:28
【问题描述】:
我的担保人将 has_many 地址建模为 :addressable 并接受地址的嵌套属性。但是,当将地址属性列入白名单时,params 哈希无法识别addresses_attributes: [:street, :city, etc.]。我不断收到未经许可的参数地址。有人有这个问题吗?
我的模型:
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
来自用户应用程序控制器
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
表单的“嵌套”部分:
<%= 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 中。有任何想法吗?提前致谢。
【问题讨论】:
-
在 Guarantor 类的
accepts_nested_attributes_for中,您不需要:address是复数吗?如果这不能解决问题,那么使用表单查看视图会很有帮助,以确保正确设置它以传递键“*_attributes”下的哈希 -
是的,在您发送消息时编辑问题。复制/粘贴错误,实际上是模型中的 :addresses 。现在将添加表单...
标签: ruby-on-rails-4 nested-attributes