Ruby on rails 注销时重定向设计轨道
轨道4.2和设计5 我在我的应用程序_controller.rb上使用它,在用户注销时将其重定向到同一页面Ruby on rails 注销时重定向设计轨道,ruby-on-rails,devise,Ruby On Rails,Devise,轨道4.2和设计5 我在我的应用程序_controller.rb上使用它,在用户注销时将其重定向到同一页面 def after_sign_out_path_for(resource_or_scope) request.referrer || root_path end 一切正常,当用户单击“注销”时,他们将注销并停留在同一页面上 但是,如果用户尝试注销失败的验证编辑配置文件*时,应用程序将抛出错误。日志显示“ActionController::RoutingError(没有与[GET]“
def after_sign_out_path_for(resource_or_scope)
request.referrer || root_path
end
一切正常,当用户单击“注销”时,他们将注销并停留在同一页面上
但是,如果用户尝试注销失败的验证编辑配置文件*时,应用程序将抛出错误。日志显示“ActionController::RoutingError(没有与[GET]“/users]匹配的路由”)
有改进代码的想法吗
*(例如,当用户试图更改用户名且用户名已被使用时,表单会给出验证错误,用户会注销)要解决您的问题,您可以如下处理路由错误:
class ApplicationController < ActionController::Base
rescue_from ActionController::RoutingError, with: :handle_routing_error
# added
def routing_error
raise ActionController::RoutingError.new(params[:path])
end
private
def handle_routing_error
redirect_to root_path, alert: "some alert text"
end
end
因此,如果您没有上次访问的url,则返回到根路径。如果您确实有上次访问的url,并且它很好,则返回到它。如果它不好(如带有验证的用例),则返回到根路径。您可以这样做
def after_sign_out_path_for(resource_or_scope)
if current_user
request.referrer
else
root_path
end
end
但我所做的是在应用程序\u controller.rb中的方法的注销\u路径\u之后使用相同的
def after_sign_out_path_for(resource_or_scope)
request.referrer || root_path
end
class UsersController < ApplicationController
before_action :authenticate_user!, only: [:show, :edit, :update, :destroy]
def show
end
end
应用程序\u controller.rb
def after_sign_out_path_for(resource_or_scope)
request.referrer || root_path
end
class UsersController < ApplicationController
before_action :authenticate_user!, only: [:show, :edit, :update, :destroy]
def show
end
end
然后在\u操作之前添加一个:使用需要user
的所有方法在控制器中验证\u user!
回调。如下所示
用户\u controller.rb
def after_sign_out_path_for(resource_or_scope)
request.referrer || root_path
end
class UsersController < ApplicationController
before_action :authenticate_user!, only: [:show, :edit, :update, :destroy]
def show
end
end
class UsersController
这样,当用户在编辑配置文件页面上注销时,它将尝试将其重定向到同一页面,但在调用users\u controller.rb
中的edit
操作之前,用户将自动重定向到登录页面
这绝对是我喜欢的,但您可能不想要它,所以您应该使用第一个解决方案。您好,谢谢您的回答。我尝试了您的第一个解决方案,不幸的是,它仍然抛出相同的错误。我还没有创建user\u controller.rb。我正在使用designe user controller更改配置文件。它看起来像是request.referer在不知道需要采取的控制器或操作的情况下捕获链接。修补程序/用户(:格式)设计/注册#更新rry,我已经更新了我的答案,在"注销"后使用更新的
路径"方法使其生效。嗨..更新了你的答案,但现在当用户注销时,所有内容都重定向到根路径..暂时我正在使用脏黑客如果URI(request.referer)。路径=“/users“
然后使用请求引用器重定向到root-else。在methodhi.的after\u sign\u out\u path\u中。。试试这个。但我可以让它工作。抱歉:(有人说rescue可能不起作用[@Aipack.噢,对不起!我忘了一个小代码段,你应该添加到routes.rb和ApplicationController中的一个方法。我的错。更新了答案。现在试试!@Aipack.P.S。我的答案现在在我自己的项目中起作用。所以它已经完全测试过了。