7

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

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
4

1 回答 1

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

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

于 2012-10-02T18:17:37.410 回答