Rails / Devise - 使用link_to自定义flash消息(devise.en.yml)

时间:2010-10-24 11:45:29

标签: ruby-on-rails devise

我想自定义devise提供的以下flash msg 在devise.en.yml文件中:

devise:
   failure:
      unconfirmed: 'You have to confirm your account before continuing.'

使用ruby代码获取指向new_user_confirmation_path的链接。

换句话说,我希望我的flash消息显示如下:

'You have to confirm your account before continuing. Didn't receive confirmation instructions?'

哪里'没有收到确认指示?'是new_user_confirmation_path的链接。

我想知道我是否可以在不编辑用户控制器的情况下执行此操作,因为默认情况下Devise不会提供它。

感谢您的回答!

5 个答案:

答案 0 :(得分:18)

new_user_confirmation_path是一个等同于/users/confirmation/new

的静态网址

所以你可以在devise.en.yml文件中执行此操作:

devise:
  failure:
    unconfirmed: "You have to confirm your account before continuing. <a href='/users/confirmation/new'>Didn't receive confirmation instructions?</a>"

在您显示闪光灯的控制器操作中,请确保您有.html_safe,例如flash[:error].html_safe

答案 1 :(得分:3)

在我的一个应用程序中,我正在使用Devise和CanCan。我通过我的应用程序控制器中的以下内容来解决CanCan的错误。

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

也许你可以做类似的事情并从Devise拯救?您的Flash消息可能类似于:

flash[:error] = t("devise.failure.unconfirmed") + link_to "Didn't receive confirmation instructions?", new_user_confirmation_path

如果需要的话,更好的是将“没有收到......”放在它自己的翻译yml中。

答案 2 :(得分:2)

我认为Rails 3&amp; S的正确方法Rails 4为Devise flash消息添加所需的链接和其他信息就是编写自己的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

并为翻译添加插值:

devise:
  failure:
    unconfirmed: You have to confirm your account before continuing.%{new_user_confirmation_link}
    unconfirmed_link_text: Didn't receive confirmation instructions?

请勿忘记添加到config/initializers/devise.rb

config.warden do |manager|
  manager.failure_app = CustomFailure
end

答案 3 :(得分:1)

如果我理解把问题放在代码的位置上('设计......你必须放在我们的视图中,你要在哪里显示这条消息。

例如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 %>

答案 4 :(得分:0)

您也可以在特定于设备的布局中执行此操作: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)