I have a model Area which has_many Streets. The Area has a couple of
attributes of its own but mostly I want to be able to add/delete/update
Streets for a particular Area, but all on one page rather than having a
separate view for editing a Street. I’ve managed to get a form
displayed with a row for each Street already assigned to the Area by
using form fields with street[id][name], street[id][num_houses] etc.
But when I send the form I don’t know how to get any validation errors
reported. Errors on Area are nicely displayed with the red outlines - I
think this is thanks to <%= error_messages_for ‘area’ %> being included
in the view. Can I get this to work for the multiple Street input
fields as well?
I’ve tried <%= error_messages_for ‘street’ %> (You have a nil object
when you didn’t expect it! You might have expected an instance of
ActiveRecord::Base. The error occured while evaluating nil.errors)
and <%= error_messages_for ‘street’ %> (`@street’ is not allowed as
an instance variable name)
@area = Area.find(params[:id])
@streets = @area.streets
unless params[:street].nil? Street.update(params[:street].keys, params[:street].values) end if @area.update_attributes(params[:area]) flash[:notice] = 'Area was successfully updated.' redirect_to :action => 'edit_area', :id => @area else render :action => 'edit_area' end
Thanks so much in advance for any suggestions.