代码之家  ›  专栏  ›  技术社区  ›  benoitr

Rails/devieve-自定义flash消息(devieve.en.yml),链接到

  •  15
  • benoitr  · 技术社区  · 14 年前

    我想定制以下flash msg,由devide提供 在devise.en.yml文件中:

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

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

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

    哪里“没有收到确认指示?”是指向新用户确认路径的链接。

    谢谢你的回答!

    5 回复  |  直到 14 年前
        1
  •  19
  •   Benjamin Atkin    12 年前

    new_user_confirmation_path 是一个静态url,相当于 /users/confirmation/new

    所以你可以在designe.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
  •  3
  •   astjohn    14 年前

    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[:error] = t("devise.failure.unconfirmed") + link_to "Didn't receive confirmation instructions?", new_user_confirmation_path
    

        3
  •  2
  •   antonversal    8 年前

    我认为Rails 3&Rails 4添加必要的链接和其他信息来设计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
  •  1
  •   Sebastián Palma    7 年前

    如果我明白了,把问题放在代码t('设计…你必须放在我们的视图,你想显示这个信息的地方。

    例如 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 %>
    
        5
  •  0
  •   montrealmike    10 年前

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