使用设计重定向循环after_sign_in_path_for设计3.2.x

时间:2015-03-27 15:39:24

标签: ruby-on-rails redirect ruby-on-rails-4 devise

即使我跟着the guide here

用户登录后我仍然可以获得重定向循环。

这就是我的路线:

  devise_for :users, :path_names => { :sign_up => "register", 
                                      :sign_in => "login", 
                                      :sign_out => "logout",
                                                                            :settings => "settings" },
                    :controllers => { confirmations: "confirmations", 
                                      registrations: "users/registrations", 
                                      passwords: "users/passwords" }

然后我将它添加到我的ApplicationController:

  def after_sign_in_path_for(resource)
    sign_in_url = new_user_session_url
    if request.referer == sign_in_url
      super
    else
      stored_location_for(resource) || request.referer || root_path
    end
  end

然后我将这些方法添加到Users/RegistrationsControllerUsers/PasswordsController

然而,当我登录时,它仍然告诉我有一个重定向循环并抛出错误。

思想?

4 个答案:

答案 0 :(得分:1)

试试此代码

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

将用户重定向到引荐来源总是一个坏主意......

答案 1 :(得分:0)

我使用以下辅助方法解决了同样的问题:

def back_or_default_path(default = root_path)
  referer = Addressable::URI.parse(request.env['HTTP_REFERER'])
  request_uri = Addressable::URI.parse(request.env['REQUEST_URI'])
  prohibited_paths = [request_uri.path]
  back = if params[:return_to].present?
    params[:return_to]
  elsif session[:return_to].present?
    session.delete(:return_to)
  elsif referer && !prohibited_paths.include?(referer.path) && root_url =~ /#{referer.host}/
    :back
  end

  back || default
end

将其放在ApplicationController内。现在您可以像这样使用它:

def after_sign_in_path_for(resource)
  back_or_default_path(super)
end

答案 2 :(得分:0)

我的应用程序中的以下代码工作正常。

将以下内容放入应用程序控制器或基本控制器

before_filter :store_location


  private

    def store_location

      # store last url - this is needed for post-login redirect to whatever the user last visited.

      return unless request.get?

      if (request.path != "/users/sign_in" &&

          request.path != "/admins/sign_in" &&

          request.path != "/admin" &&

          request.path != "/users/sign_up" &&

          request.path != "/users/password/new" &&

          request.path != "/users/password/edit" &&

          request.path != "/users/confirmation" &&

          request.path != "/users/sign_out" &&

          !request.xhr?) # don't store ajax calls

        session[:previous_url] = request.fullpath

      end

    end


    def after_sign_in_path_for(resource)

      session[:previous_url] || root_path

    end


    def after_sign_out_path_for(resource)

      session[:previous_url] || root_path

    end

另一件事,我认为request.refferer返回路径而不是网址。不确定。

答案 3 :(得分:0)

尝试在application_controller.rb

中使用此功能
before_filter :store_location
def store_location
  # store last url - this is needed for post-login redirect to whatever the user last visited.
  if (request.fullpath != "/users/sign_in" &&
      request.fullpath != "/users/sign_up" &&
      request.fullpath != "/users/password" &&
      request.fullpath != "/users/sign_out" &&
      !request.xhr?) # don't store ajax calls
    session["user_return_to"] = request.fullpath 
  end
end

def after_sign_in_path_for(resource)
  session[:user_return_to] || root_path
end