2

私は電子商取引アプリケーションに取り組んでいます。ユーザーがアプリにログインするときに、外部のサブスクリプション ハンドラーをチェックして、サブスクリプションがまだ有効であり、有効期限が切れていたり、失敗したりしていないことを確認したいと考えています。

initializers/devise.rbログイン後にモデルのチェックを実行するために、Warden コールバックを使用する方法を理解することに成功しました。ただし、問題が発生した場合は、もう一度ログアウトして、次に何をすべきかを示す特定のページにリダイレクトしたいと考えています。

これが私が持っているものです。コールバックから使用できないことはわかってredirect_toいます。それを考えると、私がやろうとしていることを行うための最良の方法は何ですか?

Warden::Manager.after_authentication do |user, auth, opts|
  begin
    user.check_active_subscription # this works, and will raise one of several exceptions if something is goofy
  rescue
    redirect_to "/account/expired" # obviously this won't work, but see what I'm trying to do?
  end
end
4

1 に答える 1

8

コールバックに例外を発生させ、コントローラーでそれから救出するだけです。例えば:

Warden::Manager.after_authentication do |user, auth, opts|
  user.check_active_subscription
end

class SessionsController < ApplicationController
  def create
    # Authenticate
  rescue SubscriptionExpiredException
    # Logout
    redirect_to "/account/expired"
  end
end

次のように使用することもできrescue_fromます。ApplicationController

class ApplicationController
  rescue_from SubscriptionExpiredException, :with => :deny_access

  def deny_access
    redirect_to "/account/expired"
  end
end
于 2011-08-19T05:52:43.143 に答える