简体   繁体   中英

How to redirect to another page after devise sign in?

First, see my routes :

Rails.application.routes.draw do
  require 'sidekiq/web'
  mount Sidekiq::Web => '/sidekiq'

  devise_for :users, controllers: {
                registrations: "registrations",
                sessions: "sessions"
  }


  devise_scope :user do
    authenticated :user do
      root 'appointments#index', as: :authenticated_root
    end
    unauthenticated do
      root 'sessions#new', as: :unauthenticated_root
    end
    match '/logout', :to => 'devise/sessions#destroy', via: :all
  end

  resources :appointments do
    get :available_slots, on: :collection
    resources :notes
    resources :images, only: [:show]
  end

  #patch 'appointments/:id' => "appointments#update_status", as: :update_status
  match 'appointments/:id/update_status' => "appointments#update_status", :via => :post
  match 'appointments/:id/visited_patient_appointment' => "appointments#visited_patient_appointment", :via => :post
  get 'archive' => "appointments#archive"


end

Now, how to redirect to appointments_path after user sign in? There is one devise method called after_sign_in_path_for(resource) which I override in Appointments Controller but still it is not working.

You trying to override in Appointments Controller which is wrong, it will be sessions_controller.rb or application_controller.rb

Try the following in the sessions_controller.rb or application_controller.rb

protected

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

If not have stored_location then he will redirect to appointments_path

If you need to redirect all time to the same page like appointments_path then

protected

def after_sign_in_path_for(resource)
    appointments_path
end

See the Devise wiki

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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