Question

I have a Rails 3.2 app running with Devise 2.3. I kept the email address as the default authentication key. When a user tries to create a new account with an existing email address, I would like to show him links to the login and to the reset password pages.

There is an answer to another question that suggests how to proceed with this in Rails, but it doesn't solve the problem when using Devise. When trying to define a custom validation on the email, the server won't even start. See the pastebin.

Is there a way I can override or hijack the error message added by Devise to use my own instead? Or is there a way to add global interpolation values?

This is the message I would like to show:

  taken: email already used, please <a href='%{login_link}'>login</a> or <a href='%{reset_link}'>reset your password</a>

The app tries to display it, but complains about the missing interpolation keys. How I can assign the interpolation keys with Devise already handling the validation on the email?

This may not be the canonical example of this problem, since application links are fairly static and hard-coding would be okay here. What if other truly dynamic values were to be interpolated?

Was it helpful?

Solution 2

AFAIK it is not possible as long as Devise's default validations are used by declaring the following on the authentication model:

devise :validatable

In order to use error message variable interpolation in Rails with YAML locale files when using Devise, all validations have to be declared manually and the ones provided by Devise have to be deactivated.

When trying to override a single existing validation, Rails 3.2 fails during initialization. So it's an all in or all out when using Devise validations.

OTHER TIPS

Try passing these from the user.rb file

validates :email, :uniqueness => {:login_link => Rails.application.routes.url_helpers.new_user_session_path, 
                                  :reset_link => Rails.application.routes.url_helpers.new_user_password_path}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top