1

我有Bike模型和Component模型。多種型號Component繼承:FrameChainCranksetRails belongs_to和單表繼承不行爲

當我提交我的形式,我的PARAMS是這樣的:

"bike" => { "frame" => { "id" => "4" }, "chain" => { "id" => "19" }, ... } 

在我的控制器,下面的代碼遊:

@bike = Bike.new(params[:bike]) 
> Frame(#90986230) expected, got HashWithIndifferentAccess(#81888970) 

如果我破解我的表單生成以下參數,它的工作原理:

"bike" => { "frame_id" => "4", "chain_id" => "19" ... } 

這裏是我的模型:

class Bike < ActiveRecord::Base 
    belongs_to :frame 
    belongs_to :chain 
    ... 
end 

class Component < ActiveRecord::Base 
    has_many :bikes 
end 

class Frame < Component 
end 

單個表繼承工作 - 我可以叫Frame.firstComponent.all沒有問題。

我瘋了嗎?嵌套參數不是通常的約定嗎?這就是生成的:

- f.fields_for @bike.frame do |frame| 
    = frame.hidden_field :id 

我在做什麼錯?

+2

你的'Bike'模型中有'accep_nested_attributes_for'嗎? – 2010-10-01 07:17:50

回答

2

您正在使用嵌套表單,因此如果使用accepts_nested_attributes_for標記(請參閱railscast 196/197),嵌套參數應該可以工作。

belongs_to :frame 
accepts_nested_attributes_for :frame 
+0

Facepalm ...謝謝 – nfm 2010-10-08 20:58:54