强制用户在Devise中注销

时间:2012-10-02 17:14:06

标签: ruby-on-rails ruby-on-rails-3 devise

我有一个应用程序,其中用户的成员资格已过期。

我正在设置我的before_filter文件中的applications.rb,以确保他们的成员资格在让他们进入网站之前是有效的。

在我的application.rb文件中:

before_filter :check_account

def check_account
  if user_signed_in?
     if current_user.account.expired
       flash[:error] = "Your account is expired. Please contact Navanti for renewal."
       redirect_to destroy_user_session_path
     end
  end
end

我不断收到重定向循环错误。我猜这是因为正在调用的注销页面也在执行before_filter,但是如果我放了一个except => [:users => :sign_out]它仍会抛出循环错误。

感谢您的帮助。


请求设计方法:

# DELETE /resource/sign_out

def destroy
  redirect_path = after_sign_out_path_for(resource_name)
  signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
  set_flash_message :notice, :signed_out if signed_out && is_navigational_format?  

  # We actually need to hardcode this as Rails default responder doesn't
  # support returning empty response on GET request
  respond_to do |format|
    format.any(*navigational_formats) { redirect_to redirect_path }
    format.all do
      head :no_content
    end
  end
end

1 个答案:

答案 0 :(得分:7)

尝试以下列方式查询控制器/操作:

def check_account
  return if params[:controller] == "devise/sessions" && params[:action] == "destroy"
  if user_signed_in?
     if current_user.account.expired
       flash[:error] = "Your account is expired. Please contact Navanti for renewal."
       redirect_to destroy_user_session_path
     end
  end
end

这应该消除你所拥有的重定向循环。