Home Session[:user_return_to] getting nil in redirecting back to current page after sign in
Reply: 0

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

user2789
1#
user2789 Published in June 19, 2018, 6:29 pm

I want to redirect back to current page after sign in I have devise already in my rails application. I have followed this tutorial of devise 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.

What is the problem after implementing this is explained below: I enter URL directly without logging in /tasks/1 at this point this url get saved in session[:user_return_to] then I am redirected to /users/sign_in at this point value in session[:user_return_to] gets nil because of this I cant redirect to previous page after logging in the application.

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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.249725 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO