Updating HABTM records is easy, but why isn't one_to_many (has_many and belongs_to)?
With the collection_select it is easy to update HABTM records, everthing is taken care of, besides declar-ing the variable for all records in de related tabel. But updating more than one record from a belongs_to table seems to need a lot of more coding. Or am I missing something?
You're working too hard.
Models: class Event < ActiveRecord::Base has_many :evtimes end
The has many lets you get at and manipulate the list of Evtimes from the Event.
class Evtime < ActiveRecord::Base belongs_to :event end
In EventsController.rb def edit @event = Event.find(params[:id])
@evtimes = Event.evtimes
end
def update @event = Event.find(params[:id])
@event.evtimes << Evtime.find(params[:evtimes])
if @event.update_attributes(params[:event]) flash[:notice] = 'Event was successfully updated.' redirect_to :action => 'show', :id => @event else render :action => 'edit' end end
And in _form.rhtml <%= error_messages_for 'event' %>
<!--[form:event]--> <p><label for="event_name">Name</label><br/> <%= text_field 'event', 'name' %></p>
<p><label for="event_city">City</label><br/> <%= text_field 'event', 'city' %></p>
<p><label for="event_description">Description</label><br/> <%= text_field 'event', 'description' %></p>
<br/> <% for evtime in @evtimes do %> Starting :<%= time_select 'evtime', 'starting' %><br/> Ending :<%= time_select 'evtime', 'ending' %><br/> Action: <%= text_field 'evtime', 'movement'%><br/> Notes:<br/> <%= text_area 'evtime', 'notes' %><br/> <% end %> <!--[eoform:event]-->
There are better ways to do the view, e.g. using the form_for helper and using a partial for the Evtimes list, but that's another story. You also need to handle the fact that you are editing multiple Evtimes. For that I'd direct you to RailsCasts episodes 73-75 http://railscasts.com/episodes;archive