devise的一个故障:undefined method user_url
这个问题是在升级项目之后出现的,rails7.1,ruby 3.3.4, devise 4.9.4, 同时也修改了用户登录后跳转的代码。
A NoMethodError occurred in sessions#create: undefined method `user_url' for an instance of Devise::SessionsController
The best resource to seek is the official repo/wiki/issues, and then SO. The answer you found is out of date.
Here is the answer: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update
下面是代码,
要注意的点是, rails7.1.3.4 默认使用了turbo_frame, 登录返回的地址不能包含 turbo_frame_request个请求。
after_sign_in_path_for(resource_or_scope) 方法 里面要注意,如果没有登录后返回的地址,那就要使用super里面的方法。
有些用户 一开始就是登录的 /user/sign_in ,那样的就没有存到url ,那就需要用到 super.
我出现标题中的问题就是因为少了 这个 || super.
下面是代码,
要注意的点是, rails7.1.3.4 默认使用了turbo_frame, 登录返回的地址不能包含 turbo_frame_request个请求。
after_sign_in_path_for(resource_or_scope) 方法 里面要注意,如果没有登录后返回的地址,那就要使用super里面的方法。
有些用户 一开始就是登录的 /user/sign_in ,那样的就没有存到url ,那就需要用到 super.
我出现标题中的问题就是因为少了 这个 || super.
Just add the following in ApplicationController for versions devise > 3.2.1:
# This example assumes that you have setup devise to authenticate a class named User. class ApplicationController < ActionController::Base before_action :store_user_location!, if: :storable_location? # The callback which stores the current location must be added before you authenticate the user # as `authenticate_user!` (or whatever your resource is) will halt the filter chain and redirect # before the location can be stored. before_action :authenticate_user! private # Its important that the location is NOT stored if: # - The request method is not GET (non idempotent) # - The request is handled by a Devise controller such as Devise::SessionsController as that could cause an # infinite redirect loop. # - The request is an Ajax request as this can lead to very unexpected behaviour. def storable_location? request.get? && is_navigational_format? && !devise_controller? && !request.xhr? & !turbo_frame_request? end def store_user_location! # :user is the scope we are authenticating store_location_for(:user, request.fullpath) end end
And then to redirect after signing in, you have to override this method:
def after_sign_in_path_for(resource_or_scope) stored_location_for(resource_or_scope) || super end
阅读量: 357
发布于:
修改于:
发布于:
修改于: