3

Rails アプリケーションを devise :confirmable で構成しました。登録後、ユーザーは確認リンクが記載された電子メールを受け取ります。ユーザーがこのリンクを 2 回クリックすると、トークンが無効になります。これは、アプリケーションで予期される動作です。ただし、「app\views\devise\confirmations\new.html.erb」にあるページにユーザーを送信し、「確認トークンが無効です」というエラーが表示され、そのエラーを表示する必要があります(<%= devise_error_messages! %>)、しかし私のサインインページに。(実際には、次の 2 つの状態が考えられます。a) トークンの有効期限が切れ、ユーザーが確認されていないため、ユーザーはもう一度トークンを要求する必要があります。b)トークンの有効期限が切れ、ユーザーが確認されたため、ユーザーはサインインするだけで済みます)

このために、Devise::ConfirmationsController を継承して独自の ConfirmationsController を作成し、show メソッドをオーバーライドして、次のようにエラーが発生した場合にサインイン ページにリダイレクトできるようにしました。

class ConfirmationsController  < Devise::ConfirmationsController

  def after_confirmation_path_for(resource_name, resource)
    super
  end

  def new
    super
  end

  def after_resending_confirmation_instructions_path_for(resource_name)
    super
  end

  def create
    super
  end

  def show
    self.resource = resource_class.confirm_by_token(params[:confirmation_token])

    if resource.errors.empty?
      set_flash_message(:notice, :confirmed) if is_navigational_format?
      sign_in(resource_name, resource)
      respond_with_navigational(resource){ redirect_to
            after_confirmation_path_for(resource_name, resource) }
    else
        respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render 'devise/sessions/new'  }
    end
  end

end

そして、routes.rb には次の構成があります。

get "confirmations/show"

devise_for :users, :path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout'}, :controllers => {:confirmations => 'confirmations'}
(...)

サインイン ページに適切にリダイレクトされますが、resources.errors に存在するエラーをサインイン ページに渡すことができません。どうすればこの問題を解決できますか?

ところで、私のユーザー モデルは次のように構成されています。

devise :database_authenticatable, :registerable, :confirmable,
    :recoverable, :rememberable, :trackable, :validatable
4

1 に答える 1