Using Rails validation helpers-
validates_xxx_of :the_column, :message => "my message"
will generate a validation message :
the_column my message
Is there a way to turn-off the inclusion of the column name? (substitute xxx with any validation helper method)
I know this question is old. But just for reference in case someone else stumbles over it as I just did.
At least in Rails 3.0.x (not sure about earlier versions) you can use the leading ^ as indicated by RadBrad without the need for any gems/plugins.
Took a look at the code here:
https://github.com/rails/rails/blob/master/activemodel/lib/active_model/errors.rb#L374
You can therefore set in your en.yml file:
en:
errors:
format: '%{message}'
This means you need to also set the full error message everywhere else but I guess this is preferable. Note, and I found this confusing, the errors format is not in the ActiveRecord namespace where I generally put the rest of my error messages.
There is a customer error message gem that should do what you want
https://github.com/jeremydurham/custom-err-msg
It allows you to override the normal message construction, and define the complete message yourself like this:
:message=> "^ Your email address seems rather messed up, please try again"
Notice the ^ character, that tells rails NOT to prepend anything, just use the message exactly as defined, (except it removes the ^)
If you don't put a leading ^, then you get the normal rails generated error message.
This is the best explanation I could find.
http://adamhooper.com/eng/articles/5
Essentially, in an initializer, change the full_messages method in ActiveRecord.Errors to return full sentences (not column_name, message concatenations) if you give a :message attribute in the validation.
Update - If you try Adam's code, you have to use the en.yml property file, if not it won't work as expected. You can either do this or get around it by modifying the full_messages method further. This works for me. I added the following to an initializer (/imitializers/active_record_errors.rb)
if RAILS_GEM_VERSION =~ /^2\.3/
ActiveRecord::Errors.class_eval do
# Remove complicated logic
def full_messages
returning full_messages = [] do
@errors.each_key do |attr|
@errors[attr].each do |message|
next unless message
if attr == "base"
full_messages << message
elsif message =~ /^\^/
full_messages << $' #' Grabs the text after the '^'
else
attr_name = @base.class.human_attribute_name(attr)
full_messages << attr_name + I18n.t('activerecord.errors.format.separator', :default => ' ') + message
end
end
end
end
end
end
end
Adam also makes good arguments for modifying Rails to support this for internationalization efforts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With