views:

51

answers:

1

This code in my view triggers an error:

<% remote_form_for(mymodel) do |f| %>
  <%= f.error_messages %>

(mymodel is not an ActiveRecord object)

When I look at the error trace, I see this section which indicates that error_messages in the view translates into error_messages_for in the active_record_helper:

C:/Ruby18/lib/ruby/gems/1.8/gems/
actionpack-2.3.2/lib/action_view/
helpers/active_record_helper.rb:179:in `error_messages_for'

C:/Ruby18/lib/ruby/gems/1.8/gems/
actionpack-2.3.2/lib/action_view/
helpers/form_helper.rb:984:in `error_messages'

Why does the form_helper think that it should call the active_record_helper even though 'mymodel' is not an ActiveRecord object?

+3  A: 

The implementation of f.error_messages is in ActiveRecordHelper because it is dependent on the specific format of ActiveRecord::Errors (which, on ActiveRecord::Base subclasses is stored on mymodel.errors).

If you remove the error_messages, it should work. Alternatively, you can create your own class that has the same interface as ActiveRecord::Errors and just substitute that in this case, duck typing style.

Ben Hughes
I'm using Datamapper in place of ActiveRecord. It has its own #errors. I've been trying to compare the two classes to find discrepancies. Wish there was a simpler way to find the inconsistencies that would prevent DM's #errors from duck-typing like AR's #errors. Is a laborious eyeball comparison required? Yehuda Katz seems to agree with you: http://groups.google.com/group/datamapper/browse_frm/thread/8031fe51d6cda033?hl=en
Hola
actually if you look at the code, http://github.com/rails/rails/blob/abebdf52a3b1db58b9abf9774fa87271d480133a/actionpack/lib/action_view/helpers/active_record_helper.rb#L109 it seems to me that you only need to implement the `on` method for whatever is returned by `errors` (as seen in line 122). As long as the DM errors hash is a similar format, that is all that is needed.
Ben Hughes
also full_messages and count I think.
Ben Hughes
I see. You're searching for "errors." in active_record_helper.rb. I find 'on', 'full_messages', 'count' as well as 'is_a', 'first' and 'respond_to'. Are you excluding 'is_a', 'respond_to' and 'first' because they are from the Object class and should be automatically included? I think 'is_a' and 'respond_to' are from Object. But I thought 'first' was from Enumerable.
Hola
Datamapper also seems to have 'full_messages' and 'on' but apparently no 'count'. Wonder why they omitted 'count'. http://github.com/datamapper/dm-more/blob/459fd82a8923d7bf9c3d6ce64ab5f4bba7b867dc/dm-validations/lib/dm-validations/validation_errors.rb
Hola
'count' in AR is just an alias for size: http://api.rubyonrails.org/classes/ActiveRecord/Errors.html#M002504. Wouldn't seem to be too hard to add this to DM.
Hola
Thanks for steering me in the right direction. Appreciate it. I'll try adding a 'count' method to DM similar to AR's 'size' method.
Hola