我有一个问题,有关嵌套归因和强大的Rails 4参数。我有一个Property
模型,嵌套与Status
模型,但每个属性只有has_one
状态。问题是,当我添加/更新属性时,属性被更新,但不是状态。看看服务器日志,似乎Rails创建了状态模型,但决定只添加/编辑property_id
(外键),created_at
和updated_at
字段。这里是日志说明一点:嵌套属性强参数只更新外键,created_at和updated_at?
SQL (0.3ms) INSERT INTO "statuses" ("created_at", "property_id", "updated_at") VALUES (?, ?, ?) [["created_at", Sat, 11 Jan 2014 16:03:21 UTC +00:00], ["property_id", 9], ["updated_at", Sat, 11 Jan 2014 16:03:21 UTC +00:00]]
(1.4ms) commit transaction
注意没有未经许可...消息出现。
我确定它不是模型相关的问题,因为我试图在控制台和property.save
工作中执行相同的操作。这就指向了与强参数有关的方向。
任何想法?
这里是我的代码: 型号/ property.rb
class Property < ActiveRecord::Base
has_one :status, dependent: :destroy
accepts_nested_attributes_for :status
end
控制器/ properties_controller.rb
def create
@property = Property.new(property_params)
@property.build_status
if @property.save
redirect_to @property, :notice => "Property created successfully."
else
render :action => 'new'
end
end
private
def property_params
params.require(:property).permit(:name,
:address_line1, :address_line2, :address_city, :address_county, :address_postcode,
:structure_Notes,
status_attributes: [:id, :letting_start_date, :process, :comission, :check_in_date, :check_out_date, :notes])
end
感谢您的阅读! :)
更新:
的PARAMS过去到服务器的数据包括:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"cotr1JZPxPEKga4QbGiFMrEgM5D+UpAzZxEImW5iJvc=", "property"=>{"name"=>"Test Status Again", "address_line1"=>"", "address_line2"=>"", "address_city"=>"", "address_county"=>"", "address_postcode"=>"", "status_attributes"=>{"letting_start_date"=>"2013-01-01", "process"=>"Promoting", "commission"=>"", "check_in_date"=>"", "check_out_date"=>"", "notes"=>"fgjkhsdfkjgh sfjgh sdfhg lskdg"}}, "commit"=>"Create Property"}
这些参数看起来像传递给控制器的参数是什么?你会在日志中找到这些。 – CDub
数据包含在params中。我会在上面的帖子中更新它。 – Quin