Display link in Rails form error message

后端 未结 6 2046
执笔经年
执笔经年 2021-02-05 18:47

On our sign-up form, we validates_uniqueness_of :email

When the a user is attempting to use our sign up form and they specify an existing email address, I\'

6条回答
  •  南笙
    南笙 (楼主)
    2021-02-05 19:00

    If you're using 2.3.x, replace your call to error_messages with your own helper, written in UsersHelper. It should accept the FormBuilder or an ActiveRecord object and adjust the error message as you see fit. You could make as many customizations as you like, or it could be as simple as a gsub:

    def user_error_messages(f)
      find_error = "This email address is already in use."
      replacement = "This email address is already in use. #{link_to(...)} to reset your password"
      f.error_messages.sub(find_error, replacement).html_safe
    end
    

    If you're using Rails3, make a helper method to simply process @user.errors.full_messages before they're emitted to the view.

提交回复
热议问题