Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flash[:notice] not working with the after_sign_out_path_for - (devise)

I want to send a flash[:notice] when a user refuse the EULA terms. So it disconnect him and redirect to after_sign_out_path_for (which is my root_path which is my public#index). But when we arrive on the page, nothing appears as if the flash[:notice] was lost.

Here is my code :

disclaimer_controller.rb

class DisclaimerController < ApplicationController
  def refuse
    @user = User.find(params[:id])
    @user.update_column('accept_disclaimer', false)
    @user.update_column('sign_in_count', 0)
    @user.save
    @user.reload
    sign_out
    flash[:notice] = "test
    redirect_to root_path
  end

public.html.erb

<div class="main">
  <%= render_flash_messages %>      
  <div class="main-inner">

      <%= yield %>

  </div><!-- /main-inner -->
</div><!-- /main -->

application_helper.rb (wich work with my layout application and public)

def render_flash_messages
  render partial: "layouts/flash_messages"
end

_flash_messages.html.erb

<div class="show-notif">
  <% if notice.present? %>
    <div class="flash-message alert alert-success collapse in">
      <%= notice %>
      <button type="button" class="close" data-dismiss="alert"><i class="icon-remove"></i> <%= I18n.t('helpers.link.close') %></button>
    </div>
  <% end %>
</div>

I tested my partial _flash_messages.html.erb, and it works perfectly when there is a notice (even in my public layout).

So I tried to change the path with root_url, root_path, public_index_path and new_user_session_path but nothing works.

I also tried different coding synthax as :

  • flash[notice] = "test" then redirect_to root_path
  • redirect_to root_path, :notice => "test"
  • redirect_to root_path, notice: "test"
  • redirect_to root_path, flash
  • redirect_to root_path, :flash => { :error => "Insufficient rights!" }
  • redirect_to root_path, flash: { :error => "Insufficient rights!" }
  • redirect_to(root_path, {:flash => { :error => "Insufficient rights!" }})

(each time I tried all the different path that I listed before)

I also tried to use flash.keep or flash.keep(:notice) (I used Rails version 3.2.13) without any change.

An finally, I tried to change my route like that :

  • root :to => redirect { |p, req| req.flash.keep; "public#index" } instead of
  • root to: "public#index"

I want to notice that I've got pretty the same problem with the after_sign_in_path_for and I found the solution. The problem was that the flash[:notice] was sending in the controller but not in the redirecting of this controller (even with a flash.keep). So I just put the direct path instead of after_sign_in_path_for and that works great. But here, it's not.

What am I doing wrong?

like image 602
Kilian Avatar asked Dec 10 '25 05:12

Kilian


1 Answers

I finnally got a solution.

I set a cookie in my Disclaimer#refuse, the cookie still exists after the redirection, so I test if my cookie exists, and if it exists, I test its value and set the flash message in Sessions#new

Finally just after setting the flash message, I delete the cookie.

disclaimer_controller.rb

def refuse
  @user = User.find(params[:id])
  @user.update_column('accept_disclaimer', false)
  @user.update_column('sign_in_count', 0)
  @user.save
  sign_out
  cookies[:login] = { :value => "refuse", :expires => Time.now + 10} # just to secure
  redirect_to root_path
end

sessions_controller.rb

def new
  if cookies[:login]
    if cookies[:login] == "refuse"
      flash[:alert] = "You have to accept the EULA terms to use our application."
    elsif cookies[:login] == "signout"
      flash[:notice] = "Sign out successfully"
    end
  end
  cookies.delete(:login)
  super
end

It works really well. But, I didn't find any solution for keeping my flash (from a redirection) when a non-logged user arrives on root_path. And I don't understand why. So, if somebody can bring some light on that problem, I'm really interested in.

like image 190
Kilian Avatar answered Dec 12 '25 17:12

Kilian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!