error_messages in rails view

39 views
Skip to first unread message

Hola

unread,
Jun 20, 2009, 2:39:37 PM6/20/09
to DataMapper
In Rails, you add error messages to the view like so:

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

However, if mymodel is a Datamapper rather than an ActiveRecord model,
it produces an error:

undefined method `count' for {}:Hash

At the moment I am addressing this unfortunately by removing the call
to error_messages in the view.

Is there a better workaround?


This is the stack trace, in case it helps (I just updated to the
latest 0.10 release candidate for all dm gems).

----------------------------------------------------------------------------------------------------

C:/Ruby18/lib/ruby/gems/1.8/gems/dm-validations-0.10.0/lib/dm-
validations/validation_errors.rb:106:in `send'
C:/Ruby18/lib/ruby/gems/1.8/gems/dm-validations-0.10.0/lib/dm-
validations/validation_errors.rb:106:in `method_missing'
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/
renderable.rb:77:in `inject'
C:/Ruby18/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_view/
helpers/active_record_helper.rb:179:in `each'
C:/Ruby18/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_view/
helpers/active_record_helper.rb:179:in `inject'
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'

Yehuda Katz

unread,
Jun 20, 2009, 2:56:03 PM6/20/09
to datam...@googlegroups.com
Is #errors in 0.10 a simple Hash? It would need to duck-type like an AR errors object to work.

-- Yehuda
--
Yehuda Katz
Developer | Engine Yard
(ph) 718.877.1325

Hola

unread,
Jun 20, 2009, 5:34:38 PM6/20/09
to DataMapper

I believe that for #errors to pass muster in active_record_helper,
there needs to be three methods in
Datamapper::Validate::ValidationErrors: 'on', 'full_messages' and
'count'. Of these three methods, the only one that is missing is
'count'.

http://github.com/datamapper/dm-more/blob/459fd82a8923d7bf9c3d6ce64ab5f4bba7b867dc/dm-validations/lib/dm-validations/validation_errors.rb

In AR 'count' is an alias for 'size', which is a simple one-liner:

http://api.rubyonrails.org/classes/ActiveRecord/Errors.html#M002504
# File vendor/rails/activerecord/lib/active_record/validations.rb,
line 225
225: def size
226: @errors.values.inject(0) { |error_count, attribute|
error_count + attribute.size }
227: end

I'm not sure if there was a particular reason why 'count' was omitted
from the DM implementation - Is it safe for me to add my own 'count'
method along the lines of the 'size' method in AR or would this break
something in DM?

Ted Han

unread,
Jun 20, 2009, 5:38:25 PM6/20/09
to datam...@googlegroups.com
It shouldn't break anything in DM.

Hola

unread,
Jun 20, 2009, 5:46:46 PM6/20/09
to DataMapper
Any idea why it doesn't mirror the AR interface? Is it likely to in
the future?

On Jun 20, 5:38 pm, Ted Han <nothe...@gmail.com> wrote:
> It shouldn't break anything in DM.
>
> On Sat, Jun 20, 2009 at 5:34 PM, Hola <holacost...@gmail.com> wrote:
>
> > I believe that for #errors to pass muster in active_record_helper,
> > there needs to be three methods in
> > Datamapper::Validate::ValidationErrors: 'on', 'full_messages' and
> > 'count'. Of these three methods, the only one that is missing is
> > 'count'.
>
> >http://github.com/datamapper/dm-more/blob/459fd82a8923d7bf9c3d6ce64ab...

Yehuda Katz

unread,
Jun 20, 2009, 5:53:54 PM6/20/09
to datam...@googlegroups.com
Just FYI, in Rails3 #on was replaced with #[]. The final 0.10 should probably support both.

-- Yehuda

Lawrence Pit

unread,
Jun 21, 2009, 1:30:53 AM6/21/09
to datam...@googlegroups.com

#on still exists in Rails 3, it is aliased with #[]

There's also effort underway to have the #on method return an Error object instead of a String (where Error might inherit from String). In views you can then query each Error object for it's attribute symbol or for it's (lazily) translated error string.

http://github.com/mateomurphy/rails/blob/73ac06a165e0e255bab03f693a6158c8dc67af17/activerecord/lib/active_record/validations.rb



Lawrence

Yehuda Katz

unread,
Jun 21, 2009, 3:37:13 AM6/21/09
to datam...@googlegroups.com
Lawrence,

#on exists, but is deprecated in Rails 3.

-- Yehuda

kristian

unread,
Jun 21, 2009, 5:46:35 AM6/21/09
to DataMapper

Hola

unread,
Jun 21, 2009, 1:17:20 PM6/21/09
to DataMapper
Correct. Thanks, Kristian.

On Jun 21, 5:46 am, kristian <meier.krist...@gmail.com> wrote:
> this is patched if you use the rails_datamapper gem:
>
> http://github.com/datamapper/dm-more/blob/459fd82a8923d7bf9c3d6ce64ab...
Reply all
Reply to author
Forward
0 new messages