I’m trying to get a multi level nested form model working but have hit a
problem. In one of my models, I have a habtm association and it’s
throwing
an error on a nested form and these are check boxes with a method of
‘time_unit_ids[]’.
The nested forms work without the habtm (time_unit_ids[]) so things are
pretty much set up okay.
Here’s the set up (obviously well and truly stripped back). Hopefully
someone can see something I’m missing.
CIA -ants
Models:
event_category:
has_many :event_types
accepts_nested_attributes_for :event_types
event_type:
has_and_belongs_to_many :time_units
has_many :events
belongs_to :event_category
accepts_nested_attributes_for :events
event:
belongs_to :event_type
controller new action has …
@event_category.event_types.build
@event_category.event_types[0].events.build
Forms:
event_categories/new.html.erb
<% form … do |form| %>
<% form.fields_for :event_types do |event_type_form| %>
<% event_type_form.fields_for :events do |event_form| %>
<% @time_units.each do |time_unit| %>
<% name = time_unit.name; id = time_unit.id %>
<%= event_form.check_box 'time_unit_ids[]', { :id => name
}, time_unit.id %>
<% end %>
undefined method `time_unit_ids[]’ for #EventType:0xb6fa13b8
I fear I may need to do this but it’s not ideal …
<%= check_box_tag
‘event_category[event_types_attributes][0][time_unit_ids][]’,
time_unit.id,
{ :id => name } %>