gpt4 book ai didi

ruby-on-rails - session [:user_return_to] getting nil in redirecting back to current page after sign in

转载 作者:行者123 更新时间:2023-12-05 07:34:08 24 4
gpt4 key购买 nike

我想在登录后重定向回当前页面 我已经在我的 Rails 应用程序中设计了。我遵循了本设计教程 https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update#storelocation-to-the-rescue .

实现后的问题解释如下:我直接输入 URL 而没有登录/tasks/1 此时这个 url 被保存在 session[:user_return_to]然后我被重定向到/users/sign_in ,此时 session[:user_return_to] 中的值变为 nil,因此我无法在登录应用程序后重定向到上一页。

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

before_action :store_user_location!, if: :storable_location?
before_action :authenticate_user!
before_filter :configure_permitted_parameters, if: :devise_controller?
before_filter :prepare_exception_notifier

rescue_from ActionController::RoutingError, with: :controller_error
rescue_from ActiveRecord::RecordNotFound, with: :active_record_error


rescue_from CanCan::AccessDenied do |exception|
render file: "#{Rails.root}/app/views/pages/not_authorized.html.erb", status: 403
end

protected

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:role_id, :username, :first_name, :last_name, :address, :organization_name , :hours_in_day, :organization_id, :job_title])
devise_parameter_sanitizer.permit(:account_update, keys: [:role_id, :username, :first_name, :last_name,:organization_id, :division_id, :department_id, :email, :password, :password_confirmation, :current_password, :job_title, :avatar, :building_id, :location_id])
end

private

def storable_location?
request.get? && is_navigational_format? && !devise_controller? && !request.xhr?
end

def store_user_location!
store_location_for(:user, request.fullpath)
end

def after_sign_in_path_for(resource)
stored_location_for(resource) || root_path
end

def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end

def prepare_exception_notifier
request.env["exception_notifier.exception_data"] = {
current_user: current_user
}
end

def active_record_error(exception)
respond_to do |f|
f.html{ render file: "errors/404", status: 404 }
f.html{ render file: "errors/500", status: 500 }
f.js{ render partial: "errors/ajax_404", status: 404 }
f.js{ render partial: "errors/ajax_500", status: 500 }
end
end

end

最佳答案

在 application_controller 中尝试以下操作

class ApplicationController < ActionController::Base
after_filter :store_location

before_action :authenticate_user!

private
def store_location
# store last url as long as it isn't a /users path
session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/
end

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

它在我手上工作

关于ruby-on-rails - session [:user_return_to] getting nil in redirecting back to current page after sign in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50290309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com