【问题标题】:Rails/Devise - Customize flash message (devise.en.yml) with a link_toRails/Devise - 使用 link_to 自定义 flash 消息 (devise.en.yml)
【发布时间】:2010-10-24 11:45:29
【问题描述】:

我想定制以下由 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 不提供它。

感谢您的回答!

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    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

    【讨论】:

      【解决方案2】:

      在我的一个应用程序中,我使用的是 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
      

      也许你可以做一些类似的事情并从设计中拯救出来?您的 Flash 消息可能类似于:

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

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

      【讨论】:

        【解决方案3】:

        我认为 Rails 3 和 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
        

        【讨论】:

          【解决方案4】:

          如果我理解的话,把问题放在代码 t('devise...你必须放在我们的视图中,你想在哪里显示这个消息。

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

          【讨论】:

          • 嗨,potapuff,我将重写问题以使其更清楚。谢谢!
          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-09-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-25
            相关资源
            最近更新 更多