Ruby on rails 在designe 3.2.x的_路径_中_符号_后使用designe重定向循环
尽管我跟着 在用户登录后,我仍然会得到重定向循环 这就是我的路线:Ruby on rails 在designe 3.2.x的_路径_中_符号_后使用designe重定向循环,ruby-on-rails,redirect,ruby-on-rails-4,devise,Ruby On Rails,Redirect,Ruby On Rails 4,Devise,尽管我跟着 在用户登录后,我仍然会得到重定向循环 这就是我的路线: devise_for :users, :path_names => { :sign_up => "register", :sign_in => "login", :sign_out => "logout",
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" }
然后我将此添加到我的应用程序控制器:
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
然而,当我登录时,它仍然告诉我有一个重定向循环并抛出一个错误
想法?试试这段代码
def after_sign_in_path_for(resource)
params[:next] || super
end
将用户重定向到推荐者总是一个坏主意…我使用以下帮助器方法解决了相同的问题:
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
将其放入应用程序控制器中。现在您可以这样使用它:
def after_sign_in_path_for(resource)
back_or_default_path(super)
end
在我的应用程序中,我有以下代码可以正常工作 将以下内容放入应用程序控制器或基本控制器中
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
返回路径而不是url。不确定。尝试在应用程序\u 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
也许循环来自代码的另一部分?您的routes.rb中有其他内容吗?在is request.referer==sign\u in\u url的路径中?