views:

34

answers:

1

I have a Bike model and a Component model. Several models inherit from Component: Frame, Chain, Crankset etc.

When I submit my form, my params look like this:

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

In my controller, the following code breaks:

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

If I hack my form to generate the following params, it works:

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

Here's my models:

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

class Component < ActiveRecord::Base
  has_many :bikes
end

class Frame < Component
end

Single table inheritance is working - I can call Frame.first and Component.all without issue.

Am I going insane? Isn't the nested params the usual convention? That's what gets generated by:

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

What am I doing wrong??

+2  A: 

You are using nested forms, so nested params should work if you use the accepts_nested_attributes_for tag (see railscast 196/197).

belongs_to :frame
accepts_nested_attributes_for :frame
giraff
Facepalm... thanks
nfm