2012-12-19 22 views
0

我品牌spankin' - 新軌,所以請原諒我,如果這是一個愚蠢的問題 - 我根本無法拼湊出足夠的答案告訴我們,如果我開始不正確。開始。Rails的腳手架參考不會保存,由於大規模分配限制

  1. 我創造了一切與我Company模型rails g scaffold Company name:string description:text location_city:string location_state:string accounttype:references

  2. 我創建因此相關Accounttype模型rails g scaffold Accounttype id:primary_key name:string price:decimal

  3. Company模型中的任何包含以下準備:

    belongs_to :accounttype

    attr_accessible :description, :location_city, :location_state, :name

  4. ,當我去了公司,我的支架產生編輯表單,並在這些字段中輸入數據時,它拋出一個錯誤:Can't mass-assign protected attributes: accounttype

  5. 如果我添加:accounttypeattr_accessible,它拋出Accounttype(#70175254242100) expected, got String(#70175215042700)用下面的請求參數:

    {"utf8"=>"✓", "authenticity_token"=>"oXm3cqo0jemKYFB5OGqn5ixXLSB+bH19/1RhPUu0ZHU=", "company"=>{"name"=>"Acme Corp", "description"=>"a", "location_city"=>"san diego", "location_state"=>"california", "accounttype"=>"1"}, "commit"=>"Create Company"}

那麼,我的問題是我用:references來鏈接這兩個模型?如果那是我用過的,那麼我應該怎麼做才能創建/更新才能工作?

回答

1

恭喜使用學習的軌道!您必須在Company型號中使用accepts_nested_attributes_for才能將屬性直接分配給Accounttype型號。像這樣:

belongs_to :accounttype 

accepts_nested_attributes_for :accounttype 

attr_accessible :description, :location_city, :location_state, :name, :accounttypes_attributes 

注意,因爲你正在使用attr_accessible你必須添加accounttypes_attributes 另外,我建議改變AccounttypeAccountType

來源:DocsRailscast

+0

謝謝你傑森的完整的答案。但是,即使進行了這些更改,它仍然會拋出相同的錯誤:'不能批量分配受保護的屬性:accounttype' – Kristian