error message not presented with fields_for?

475 views
Skip to first unread message

patrick99e99

unread,
Jul 11, 2012, 2:40:51 PM7/11/12
to SimpleForm
Hi everyone,

So, I have a has_one association, and the model has
"accepts_nested_attributes_for" for that association... I also have a
validation requirement for the associate model object to be present...

So I have a form:

simple_form_for @user do
f.input :username
f.simple_fields_for :location do |location_form|
location_form.input :address, :label => "Location"
end
end

When I submit the form without a location, I see:
2 errors prohibited this user from being saved:
Username can't be blank
Location can't be blank

However, the actual html for the location input does not render a
wrapper with a field_with_errors class, nor is there a span tag with
"Can't be blank", as there is with the username input.

...

I thought perhaps I just needed to do:
f.simple_fields_for :location do |location_form|
location_form.input :address, :label => "Location"
f.error :location
end

Although, that does puts the span tag "Can't be blank" on the page, it
doesn't put it inside the location input wrapper... So I am wondering
how to do that? I tried passing it in as a block to the input, but
that didn't work either.

So I ended up doing:
f.simple_fields_for :location, Location.new do |location_form|
content_tag :div, :class => [:input, :string, :required,
(:field_with_errors if f.error(:location))] do
location_form.input :address, :label =>
"Location", :required => true, :wrapper => false
f.error :location
end
end

Which seems a little ridiculous, so I am assuming that must be a
better way to handle this..... Any suggestions?

-patrick

Rafael Mendonça França

unread,
Jul 13, 2012, 9:00:51 AM7/13/12
to plataformate...@googlegroups.com
simple_fields_for is only a delegation to fields_for and they don't handle validation erros in the association object.

However, if an association attribute is invalid it should show a div with class field_with_errors around it.

Reply all
Reply to author
Forward
0 new messages