1
votes

I have read pretty much every question here about the nested forms with has_many through associations, but I can't get my model to work. Can someone please help?

There are 2 models: archetypes and skirtpreferences, linked through a skirtpreferencing model.

Here are the models:

class Archetype < ActiveRecord::Base attr_accessible :occasion, :skirt_partworth, :title, :skirtpreferencings_attributes

has_many :skirtpreferencings has_many :SkirtPreferences, :through => :skirtpreferencings accepts_nested_attributes_for :SkirtPreferences accepts_nested_attributes_for :skirtpreferencings end

Blockquote

class Skirtpreferencing < ActiveRecord::Base attr_accessible :archetype_id, :skirt_preference_id, :skirtpreferencing_attributes

belongs_to :archetype belongs_to :SkirtPreferences
accepts_nested_attributes_for :SkirtPreferences

end

class SkirtPreference < ActiveRecord::Base attr_accessible :archetype_id, ....

has_many :skirtpreferencings has_many :archetypes, :through => :skirtpreferencings

end

The form looks like this and that is displaying just fine:

<%= form_for(@archetype) do |f| %> ... <%= f.fields_for :skirtpreferencing do |preference_builder| %> <%= preference_builder.fields_for :SkirtPreferences do |builder| %> <%= render "skirt_preferences_field", :f => builder %> <% end %> <% end %> ...

I imagine I hav to do something in the controllers, but I am not sure exactly what.

Thanks!

Adding the Controllers:

class ArchetypesController < ApplicationController

 def new
 @archetype = Archetype.new
 @archetype.skirtpreferencings.build
end

    # GET /archetypes/1/edit
def edit
  @archetype = Archetype.find(params[:id])
end

 def create
 @archetype = Archetype.new(params[:archetype])     
end 


class SkirtPreferencesController < ApplicationController   

 def new
 @skirt_preference = SkirtPreference.new
 end 

 def edit
 @skirt_preference = SkirtPreference.find(params[:id])
 end


 def create
 @skirt_preference = SkirtPreference.new(params[:skirt_preference])

 end
1
Is the form generated for the new method in ArchetypesController?Jason Kim
Please cut down your code to the problem section.Linuxios

1 Answers

1
votes

I am guessing this is many-to-many relationship between SkirtPreference and Archetype, while SkirtPreferencing is the association between SkirtPreference and Archetype?

Try changing from skirtpreferencing_attributes to skirtpreferences_attributes. That's my hunch. Because you are trying to add data to skritpreferences, not the skirtpreferencings which are just there to associate between skirtpreferences and archetypes.

I also think this is unusual.

has_many :SkirtPreference, :through => :skirtpreferencings

accepts_nested_attributes_for :SkirtPreference

...

belongs_to :SkirtPreference

accepts_nested_attributes_for :SkirtPreference

All these normally should be :skirtpreferences.


** EDIT 1 **

Assuming the forms are generated in new action in ArchetypesController...

You seem to be missing the part where you build a skirtpreferencing attribute out of archetype.

So in your new action in ArchetypesController

def new
  @archetype = Archetype.new
  @archetype.skirtpreferencings.build
  ...
end

** Edit 2 **

SkirtPreferences should be changed to skirtpreferences except for class name.

Can you try changing f.fields_for :skirtpreferencing do to f.fields_for :skirtpreferencings do