views:

758

answers:

1

I have errors in a model that are showing up in the view when accessed with @model.errors. However, they do not show up if I try to do

  <%form_for @applicant do |f|%>
      <%=f.error_messages%> 
      This is zero, freakily: <%=f.error_messages.length%> 
      But this is not: <%[email protected]%>

however, the errors are present if I do

@applicant.errors.each

Any suggestions as to where I should look to resolve this? It's absolutely strange...

The form is working perfectly otherwise.

Edit: Thanks to ScottD's answer I figured out what was happening. I had simplified the question here on SO, but that was the problem. I was really doing this:

<%form_for @applicant.thinger do |f|%>

and therein lies the problem. The error_messages_for method that the form_helper calls needs the thing to be one level deep (meaning an instance variable like @applicant, which it then translates to :applicant, and never like @applicant.status). Thanks!

+2  A: 

In this example f is not the @applicant object, it is the Form builder object. What you want is something like:

<%= error_messages_for 'applicant' %>

See the Rails API Docs.

ScottD
+1 That worked, however it makes no sense to me. I always use f.error_messages and it always works. As it says here, http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html you can use the FormBuilder instance to get the errors.
Yar
I mean, thank you for that, if you have any suggestions as to why it wouldn't work with f I'd like to know please.
Yar
I answered my own question in the question, above, and thanks to your answer.
Yar