2014-01-11 45 views
0

我有一个问题,有关嵌套归因和强大的Rails 4参数。我有一个Property模型,嵌套与Status模型,但每个属性只有has_one状态。问题是,当我添加/更新属性时,属性被更新,但不是状态。看看服务器日志,似乎Rails创建了状态模型,但决定只添加/编辑property_id(外键),created_atupdated_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"} 
+0

这些参数看起来像传递给控制器​​的参数是什么?你会在日志中找到这些。 – CDub

+0

数据包含在params中。我会在上面的帖子中更新它。 – Quin

回答

0

我想我已经找到了,为什么它不工作。

如果你看一下控制器:

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 

属性值分配状态对象建立之前。因此,property保存时没有指定状态值。

这里的新代码:

@property = Property.new 
    @property.build_status 
    @property.assign_attributes(property_params) 

希望这将帮助别人的未来......非常感谢大家谁已经看到,并试图解决这个问题!