Humming
Humming

Reputation: 453

Devise not showing success messages

Devise is behaving strangely. It shows the red or the error messages, but it does not show the green or success messages.

This happened after I made changes to the routing and controller to redirect to sign-in page or home page according to whether the user is signed-in or not.

So the routes.rb now has:

devise_for :users

root :to => "questions#redirect_on_visit" 

match 'home', :to => "questions#index"

and the controller:

before_filter :authenticate_user!, :except => [:redirect_on_visit]

def redirect_on_visit
  if user_signed_in?       
    redirect_to home_path      
  else    
    redirect_to new_user_session_path      
  end
end

Also changed application controller to redirect to appropriate pages after sign-in/sign-out:

 protected

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

 private

 def after_sign_out_path_for(resource)
   stored_location_for(:user) || root_path    
 end

Upvotes: 1

Views: 2797

Answers (2)

Humming
Humming

Reputation: 453

I was being extremely numb. The fact that I have root :to => "questions#redirect_on_visit", will redirect each time a user visits, signs in or signs out. Redirect was clearly washing away the flash messages.

Finally I found the solution I was looking for. It is to preserve the flash messages flash.keep(:notice) as mentioned here.

Upvotes: 3

Viren
Viren

Reputation: 5962

Your problem is the multiple redirection from login => root => home as flash object has a life leave only for a single request

I guess It would be better of writing redirect_on_visit routing in after_sign_in_path_for and after_sign_out_path_for

something like this

def after_sign_in_path_for(resource)    
   stored_location_for(:user) ||  home_path    
 end

 private

 def after_sign_out_path_for(resource)
   stored_location_for(:user) || new_user_session_path   
 end

Upvotes: 0

Related Questions