Getting non interpolated string from ActiveRecord::Errors

15 views
Skip to first unread message

Ralphb

unread,
Oct 16, 2009, 10:32:03 AM10/16/09
to rails-i18n
Hello
I am actually working on an application that needs error messages tied
more to context (vue) than to ActiveRecord Objects ie : user can be
"client" in one view, "administrator" in an other view and "user" in
the last one. Of course in multiples langages.

Errors are all rendered in one partial (same html structure on all
pages)

I was thinking that the following way of working could suit this
need :
- for each error on a page, we get the uninterpolated message (ie:
models.user.attributes.email.blank)
- we have the view name and so check if
(uninterpolated_error, :scope => [:view_nam]), which is the more
specific, exists, if not, we use uninterpolated_error to get
translation.

My first question is "Is this the best way of doing this ?"

Second things, I wrote a small plugin in order to redefine
generate_message(attribute, message = :invalid, options = {}) in
ActiveRecord::Errors to store the uninterpolated string in a instance
var, but even if I can add function to ActiveRecord::Errors I can not
replace generate_message method. I tried all way I could think of
(reopening class and module, using class_eval, ask to include and/or
extend a module with the new definition...) and could not have it
done ! I know this is maybe not the best place for this, but any help
would be appreciated !

While trying to find a way to get this information I also saw that an
ActiveRecord:errors object own in its @errors variable a hash with
arrays of ActiveRecord::Error (singular) objects as values (see
below). This object has everything I need for this and I was thinking
about using it but... I do not find its definition anywhere in Rails
sources (where should I look ?) and I never sleep the first night with
a class for which I can not read definition...

Note : example of what I said above :
@errors=
{"email"=>
[#<ActiveRecord::Error:0x23afc08
@attribute=:email,
@base=
#<User id: 119800407, email:"abc" ...... >,
@message="should look like an email address.",
@options={:value=>"abc"},
@type=:invalid>]}>

Thanks
Raphaël.
Reply all
Reply to author
Forward
0 new messages