Have you read the validation documentation?
All of the Rails validators support a :message option, for example
look at validates_presence_of:
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M002164
See where it says "message - A custom error message (default is:
"can‘t be blank")." ? That means when you write
validates_presence_of :foo, :message => "Message can't be blank!"
--
Greg Donald
http://destiney.com/
And when saving a user a company is also saved So in user model I
have
validates_associated :company
The solution works perfectly for user model attributes. But it
is not working for company attributes. For example in companies table i
have address_city field And as the solution suggests I add to company
model below code
HUMANIZED_COLUMNS = {:address_city => "City"}
def self.human_attribute_name(attribute)
HUMANIZED_COLUMNS[attribute.to_sym] || super
end
But this is not working It shows Address city .But I need is
City. Please help
Thanks
Tom
--
Posted via http://www.ruby-forum.com/.
module ActiveRecord::Validations::ClassMethods
def validates_associated(*associations)
associations.each do |association|
class_eval do
validates_each(associations) do |record, associate_name,
value|
associates = record.send(associate_name)
associates = [associates] unless
associates.respond_to?('each')
associates.each do |associate|
if associate && !associate.valid?
associate.errors.each do |key, value|
record.errors.add(key, value)
end
end
end
end
end
end
end
end
> module ActiveRecord::Validations::ClassMethods
> def validates_associated(*associations)
> associations.each do |association|
> class_eval do
> validates_each(associations) do |record, associate_name,
> value|
> associates = record.send(associate_name)
> associates = [associates] unless
> associates.respond_to?('each')
> associates.each do |associate|
> if associate && !associate.valid?
> associate.errors.each do |key, value|
humanized_columns = {:address_city =>
"City",:phone_oofice => "Office Phone Number"}
record.errors.add(key, value,{:attribute =>
humanized[key.to_sym] || human_attribute_name(key.to_s)})
> end
> end
> end
> end
> end
> end
> end
> end
Thanks
Tom
en:
activerecord:
models:
user: "User"
company: "Company"
attributes:
user:
login: "Username"
password. "Password"
company:
name: "Company name"
I prefer this method over having the HUMANIZED_COLUMNS in every model.
I18n can also be used to customize error messages, if you dont like the
default ones (like "is invalid"). This is certanly a lot cleaner than
hacking active record.
Tom
Unfortunately, Rails' official I18N is quite cumbersome. I highly
recommend fast_gettext.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org