ruby on rails - Build custom error messages in model files (example: using the 'pluralize' method) -


i using ruby on rails 3.0.7 , use pluralize method in model file in order build custom error messages.

for example, following:

name_min_lenght = 2 # value '2' plan change (maybe dynamically... if possible) in future development  validates :name,   :length     => {     :minimum        => name_min_lenght,     :too_short      => "is short (minimum #{pluralize(name_min_lenght, 'character')})",   }, 

how can that? advisable? why?

i'm inclined agree wukerplank looks bit overengineered, it's still interesting question. according this post can use helpers in controller methods:

put in class applicationcontroller:

 def     helper.instance   end    class helper     include singleton     include actionview::helpers::texthelper   end 

then can use so:

def check_for_max_donkeys     if donkey.find_fit_donkeys.size == app_settings['max_fit_donkeys']       flash_error "the maximum of #{help.pluralize(app_settings['max_fit_donkeys'], 'donkey')} has been reached."       redirect_to_index     end   end 

i think similar should work inside validation method, i've not tried out - leave note if works please :-)


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -