2014-01-11 48 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) 

希望這將幫助別人的未來......非常感謝大家誰已經看到,並試圖解決這個問題!