I would like to customize the following flash msg provided by devise in the devise.en.yml file:
devise:
failure:
unconfirmed: 'You have to confirm your account before continuing.'
with ruby code in order to get a link to new_user_confirmation_path.
in other words, I want my flash message displays something like :
'You have to confirm your account before continuing. Didn't receive confirm开发者_运维技巧ation instructions?'
where 'Didn't receive confirmation instructions?' is a link to new_user_confirmation_path.
I would like to know if I can do this without editing the user controller cause Devise doesn't provide it by default.
Thanks for your answer!
new_user_confirmation_path
is a static url equivalent to /users/confirmation/new
So you can just do this in your devise.en.yml file:
devise:
failure:
unconfirmed: "You have to confirm your account before continuing. <a href='/users/confirmation/new'>Didn't receive confirmation instructions?</a>"
In the controller actions where you display your flash make sure you have .html_safe
e.g. flash[:error].html_safe
In one of my apps I am using Devise and CanCan. I rescue from CanCan's errors with something like the following in my application conroller.
rescue_from CanCan::AccessDenied do |exception|
if current_user
flash[:error] = exception.message
redirect_to root_url
else
flash[:error] = t("devise.failure.unauthenticated")
redirect_to new_user_session_path
end
end
Perhaps you could do something similar and rescue from Devise? Your flash message could then be something like:
flash[:error] = t("devise.failure.unconfirmed") + link_to "Didn't receive confirmation instructions?", new_user_confirmation_path
Even better would be to put the "Didn't receive..." in it's own translation yml if need be.
I think the right way for Rails 3 & Rails 4 to add required links and other information to Devise flash messages is to write your own Devise::FailureApp
:
# lib/custom_failure.rb
class CustomFailure < Devise::FailureApp
def i18n_options(options)
path = Rails.application.routes.url_helpers.new_user_confirmation_path
link = ActionController::Base.helpers.link_to(I18n.t('.unconfirmed_link_text', options), path)
super(options).merge(new_user_confirmation_link: link)
end
end
And add interpolation to translations:
devise:
failure:
unconfirmed: You have to confirm your account before continuing.%{new_user_confirmation_link}
unconfirmed_link_text: Didn't receive confirmation instructions?
don't forget to add to config/initializers/devise.rb
:
config.warden do |manager|
manager.failure_app = CustomFailure
end
If I'm understand put question right the bit of code t('devise...you must place to our view, where you want to show this message.
For example devise/new.erb
:
<%= t('devise.failure.unconfirmed',
:confirm_link => link_to(
t('devise.failure.confirm_link_text'),
new_user_confirmation_path).html_safe
) unless user.confirmed %>
You can also do it in your devise specific layout: app/views/layouts/devise.html.slim
- flash.each do |type, message|
- if ['alert', 'error', 'notice'].include?(type.to_s)
.alert-box class=type
=> message
- case message
- when t('devise.failure.unauthenticated')
= link_to "Forgot your password?", new_password_path(resource_name)
- when t('devise.failure.unconfirmed')
= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name)
- when t('devise.failure.locked')
- if resource_class.unlock_strategy_enabled?(:email)
= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name)
精彩评论