我无法在Rails 4中使用accepts_nested_attributes_for来处理强参数。这是错误
Processing by CityaddressesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Cx9nunLAsHkvo/Z8vKnWFnrub1LpmUgVNdePcQ9VDSQ=", "cityaddress"=>{"st_unit"=>"1", "st_num"=>"1", "st_prefix"=>"E", "name"=>"1", "st_type"=>"ST", "st_postalcode"=>"", "description"=>"", "cityaccount"=>{"name"=>"1", "description"=>"1"}}, "commit"=>"Create Cityaddress"}
[1m[36mUser Load (0.0ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1[0m
Unpermitted parameters: cityaccount
这是模型:
class Cityaddress < ActiveRecord::Base
has_many :cityaccounts
has_many :license_plates
accepts_nested_attributes_for :cityaccounts, allow_destroy: true
def street_address
return "#{st_unit} #{st_num} #{name} #{st_type} #{st_prefix}"
end
def address
return "#{st_unit}#{st_prefix} #{name} #{st_type} #{st_num} "
end
end
这是控制器:
# GET /cityaddresses/new
def new
@cityaddress = Cityaddress.new
@streets = Street.where("active=1").order("display_order")
@cityaddress_accounts = @cityaddress.cityaccounts.build
end
def cityaddress_params
params.require(:cityaddress).permit(:st_unit, :st_num, :st_prefix, :name, :st_type, :st_postalcode, :description, cityaccounts_attributes: [:name, :description, :id])
end
这是观点:
<%= f.fields_for @cityaddress_accounts do |ff| %>
<div class="field">
<%= ff.label :name %>
<%= ff.text_field :name %><br>
</div>
<div class="field">
<%= ff.label :description %>
<%= ff.text_field :description %><br>
</div>
<% end %>
我认为这与强参数语法有关?
干杯。
答案 0 :(得分:2)
我认为你必须改变观点。
尝试更改此
<%= f.fields_for @cityaddress_accounts do |ff| %>
到
<%= f.fields_for :cityaccounts do |ff| %>
答案 1 :(得分:1)
似乎一切正确,但在Controller中尝试这样做:
def new
@cityaddress = Cityaddress.new
@streets = Street.where("active=1").order("display_order")
@cityaddress.cityaccounts.build
end