ruby on rails - Custom validation message (in model file) that includes a path not working -
in user model file have included:
validates :email, uniqueness: { case_sensitive: false, message: "you can reset password " + link_to("here", new_password_reset_path) }
on loading page on development server, generates error:
undefined local variable or method `new_password_reset_path' #<class:0x007f4d36863460>
the alternative code below has same result/error message.
validates :email, uniqueness: { message: "you can reset password #{link_to("here", new_password_reset_path)}" }
is not possible refer path in model file? if not, how implement intended here?
update: @ first seemed solutions included rails.application.routes.url_helpers
in model file worked. , indeed in development worked. however, turned out, don't work in production. crash site on heroku. this post seems refer similar , seems indicate should not use rails.application.routes.url_helpers
in model file. after pushing heroku, when try open website says "application error". checked heroku logs , found:
app[web.1]: [3] ! unable load application: nomethoderror: undefined method `new_password_reset_path' #<module:0x007f7392350b08> app[web.1]: /app/app/models/user.rb:32:in `<class:user>': undefined method `new_password_reset_path' #<module:0x007f7392350b08> (nomethoderror)
does have alternative solution works on heroku / in production? that is, solution how include url/path in custom validation message in model file.
you can try this. hope help.
config/locales/en.yml
en: activerecord: errors: messages: email: 'you can reset password <a href="%{link}">here</a>.'
in user model.
validates :email, uniqueness: { case_sensitive: false, message: i18n.t("activerecord.errors.messages.email", link: rails.application.routes.url_helpers.new_password_reset_url) }
Comments
Post a Comment