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 :SkirtPreferencesend
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
new
method in ArchetypesController? – Jason Kim