Search code examples
ruby-on-railsturbolinks

Preventing flash from loading on undesired pages while using Rails and Turbo Links


I have a problem with my flash message lingering on pages that I don't want it to appear on.

I'm using Rails 4 with turbo links.

While I very much like turbo links, I think it might be the problem here.

In my rails controller I'm setting the desire flash as follows:

def show
     if item.status == "In Progress" || item.status == "draft"
            flash[:alert] = "Please remember: the status of this text is draft. You have been granted access through the generosity of the editor. Please use the comments to help make suggestions or corrections."
     end
 end

But then, when I click a link and turbo links responsively loads a new page, the flash appears again.

Any ideas about how to prevent this while continuing to use turbolinks


Solution

  • You can either clear the flash message in before_action in the application controller In application_controller.rb

    before_action :clear_flash
    
    def clear_flash
      flash.discard
    end
    

    Or simply use change your code to:

    def show
     if item.status == "In Progress" || item.status == "draft"
            flash.now[:alert] = "Please remember: the status of this text is draft. You have been granted access through the generosity of the editor. Please use the comments to help make suggestions or corrections."
     end    
    end