Problems with error_messages_for


#1

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)

def update_area
@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

end

Thanks so much in advance for any suggestions.


#2

On Jan 23, 2006, at 6:39 AM, Michael Bannister wrote:

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?

error_ messages_for is not responsible for adding the red outlines–
its sole purpose is to dump out the error messages associated with an
ActiveRecord object. In fact, it is highly recommended that you use
your own method or replace error_messages_for because it is so ugly
(imho).

To answer your question, though, see http://www.ruby-forum.com/topic/
52143. The topic is closely related, and then goes off-topic a bit
towards your direction, so keep reading.

Duane J.
(canadaduane)
http://blog.inquirylabs.com/


#3

To answer your question, though, see http://www.ruby-forum.com/topic/
52143. The topic is closely related, and then goes off-topic a bit
towards your direction, so keep reading.

Many thanks - looks like what I’m after, and I’ll read it more
thoroughly tomorrow! :slight_smile: