4

この質問のように、ユーザーがメールアドレスを確認せずにログインしてサイトを使用できるようにDeviseを設定しています。ただし、サイトには、確認しないとユーザーが使用できない機能がいくつかあります。

OK、それは大丈夫です。確認できcurrent_user.confirmed?ます。確認されていない場合は、ページにボタンを配置して、確認の再送信をリクエストしてもらうことができます。

私が抱えている問題は、ログイン中にこれを行うと、結果ページに表示されるフラッシュメッセージが「既にサインインしています」ということです。これは理想的ではありません。確認が送信されたというメッセージを表示したいだけです。

私は、Devise::ConfirmationControllerオーバーライドするメソッドとその方法を理解しようとする道を歩み始めていますが、誰かがすでにこれを行っていることを望んでいます。

4

4 に答える 4

7

フラッシュに「すでにサインインしています」と表示されるのは、ユーザーがメソッドnew_session_pathからリダイレクトされているためです。after_resending_confirmation_instructions_path_forこのメソッドをオーバーライドして、ログインしているかどうかを確認します。ログインしている場合は、にリダイレクトせずnew_session_path、フラッシュメッセージを設定して、別のページにリダイレクトします。

確認コントローラーを挿入してオーバーライドしますcontrollers/users/confirmations_controller.rb

class Users::ConfirmationsController < Devise::ConfirmationsController

  protected

  def after_resending_confirmation_instructions_path_for(resource_name)
    if signed_in?
      flash[:notice] = "New message here" #this is optional since devise already sets the flash message
      root_path
    else
      new_session_path(resource_name)
    end
  end
end

確認コントローラーをルートに追加します->

devise_for :users, :controllers => {:confirmations => 'users/confirmations' }
于 2012-10-22T16:35:21.273 に答える
1

私はそれがこのように見えるべきだと思います:

module Devise
  module ConfirmationsController
    extend ActiveSupport::Concern

    included do
      alias_method_chain :show, :new_flash
    end

    def show_with_new_flash
      # do some stuff
      flash[:notice] = "New message goes here"
    end
  end
end
于 2012-10-18T05:42:00.170 に答える
0

編集できます

config / locales / devise.en.ymlは、次の行でより関連性が高くなります。

failure:
  already_authenticated: 'You are already signed in.'

または、フラッシュメッセージが追加されたビューでこれを行うことができます

<%=content_tag :div, msg, id: "flash_#{name}" unless msg.blank? or msg == "You are already signed in."%>
于 2012-10-22T17:19:09.217 に答える
0

私はDevise3.1.0を使用していますが、このシナリオには、投票数の多い回答で説明されているafter_resending_confirmation_instructions_path_forの代わりに別の方法があります。私はそのように私のものを変更しました:

class Users::ConfirmationsController < Devise::ConfirmationsController

  protected

  def after_confirmation_path_for(resource_name, resource)
    if signed_in?
      set_flash_message(:notice, :confirmed)
      root_path
    elsif Devise.allow_insecure_sign_in_after_confirmation
      after_sign_in_path_for(resource)
    else
      new_session_path(resource_name)
    end
  end
end
于 2013-09-13T18:29:05.677 に答える