2015-03-27 3 views
1

Несмотря на то, что я следовал the guide here.Переадресованный цикл с разработкой after_sign_in_path_for Devise 3.2.x

Я все еще получаю цикл редирект после того как пользователь вошел в систему

Это то, что мои маршруты выглядит следующим образом:.

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/RegistrationsController, так и для Users/PasswordsController.

Однако, когда я вхожу в систему, он все еще говорит мне, что существует цикл перенаправления и выдает ошибку.

Мысли?

+0

Может цикл приходит из другой части кода? У вас были другие вещи на ваших маршрутах. Rb? – Chambeur

+0

В after_sign_in_path_for is request.referer == sign_in_url? – brenzy

ответ

1

Попробуйте этот код

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

Его всегда плохая идея, чтобы перенаправить пользователя на ссылающихся ...

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 
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 != "https://stackoverflow.com/users/sign_in" && 

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

          request.path != "/admin" && 

          request.path != "https://stackoverflow.com/users/sign_up" && 

          request.path != "https://stackoverflow.com/users/password/new" && 

          request.path != "https://stackoverflow.com/users/password/edit" && 

          request.path != "https://stackoverflow.com/users/confirmation" && 

          request.path != "https://stackoverflow.com/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 возвращает путь и не URL. Не уверен.

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 != "https://stackoverflow.com/users/sign_in" && 
     request.fullpath != "https://stackoverflow.com/users/sign_up" && 
     request.fullpath != "https://stackoverflow.com/users/password" && 
     request.fullpath != "https://stackoverflow.com/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 
Смежные вопросы