1

当用户访问他们无权查看的页面时,他们会被重定向到登录页面。他们登录后会被重定向到站点根目录。有没有一种快速简便的方法可以将他们重定向回他们最初要求的页面?

4

2 回答 2

3

您可以在-form 中添加一个隐藏字段referring_page,以将引用者添加到该字段并在其存在时路由回该字段,如下所示:sign_informer

def after_sign_in_path_for(resource)
  params[:referring_page] || super
end

这是一个手动解决方案,但我更喜欢在控制器端使用复杂的逻辑。

于 2012-06-30T10:56:48.587 回答
1

我不明白你到底想通过说“如果用户访问他们无权查看的页面,他们会被重定向到登录页面。登录后他们被重定向到站点根目录”

我假设会有两组用户,一组是管理员用户,另一组是非管理员用户,我处理这个问题的方式是这样的,

内部应用程序控制器

class ApplicationController < ActionController::Base
  protect_from_forgery

  check_authorization :unless => :devise_controller?
  before_filter :authenticate_user! 

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to root_url, :alert => exception.message
  end

  protected

  def stored_location_for(resource_or_scope)
    nil
  end

  def after_sign_in_path_for(resource_or_scope)
    if current_user.admin?
      #add your desired path
    else
      #redirect to your desired path
    end
  end
end
于 2012-06-30T05:52:36.587 回答