繁体   English   中英

登录后会话[:user_return_to]无效,无法重定向回当前页面

[英]Session[:user_return_to] getting nil in redirecting back to current page after sign in

登录后,我想重新引导回到当前页面。 我遵循了关于设计的本教程https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in、-sign-out、-sign-向上,更新#storelocation至救援

实施此命令后出现的问题是什么,如下所述:我直接输入URL而不登录/ tasks / 1,此时该URL保存在session [:user_return_to]中,然后我在会话中的此点重定向到/ users / sign_in [: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

它在我手上

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM