Search code examples
ruby-on-railsruby-on-rails-5turbolinksrails-flash

Rails redirect flash[:notice] persisting when not supposed to


I have a button with a redirect_to that runs my controller action, which creates a new row for my model. The first time, it creates it correctly and redirects. After my redirect, I go to the previous page through the menu and repeat the same action. After I click on the button, it redirects to the correct page (which it shouldn't yet... caching?), and then my previous flash message displays. So it's duplicated. I put a debugger statement in to see where it happens on the second run through - it actually happens before my button action executes. After running the rest of the code, it redirects (but since it redirected prematurely, redirects to same page) correctly with the corresponding (second) flash. How do I get rid of the extra initial flash message?

Here's a picture showing what I mean:

enter image description here

If you look at the blue loading bar right underneath the URL, it shows that the page hasn't loaded yet (I stopped it with debugger statement as shown below). However, the redirect and flash has already happened, which it isn't supposed to, since the intended redirect and flash will happen after turbolinks finishes the page load.

Initial link:

<%= link_to create_wager_from_favorite_wager_path(favorite_wager), data: { confirm: 'Create this wager?' } do %>
  Create Wager
<% end %>

Controller action:

def create_wager_from
  debugger
  # on second run through, the redirect and flash happens before I reach this point
  @favorite_wager = FavoriteWager.find(params[:id])
  @anchor = params[:anchor]

  set_member_statuses()
  result_message = @favorite_wager.create_wager_from_favorite(current_user)

  respond_to do |format|
      format.html { redirect_to my_wagers_path(:game => @favorite_wager.game), notice: "Wager created successfully!" }
  end
end

At this point, it follows the standard path and I'm 99% sure the rest of the code is irrelevant.

I've tried checking the flash params at the point that the page with the button action loads, but it's empty. So I'm not sure what's causing this issue. Any insight appreciated.

Update: changing to flash.now[:notice] makes the duplicates stop, but the flash only displays on the first click of the button. Then it doesn't appear anytime after. And refreshing the page will allow the error to be repeated.


Solution

  • After reading up on Turbolinks, I'd determined that the cause of this issue is the natural built-in functionality in turbolinks called a "page preview". This is where it will display the previous cached page as a sort of "preview" before the server response arrives, which gives the illusion that the page already loaded.

    However, the cached redirect page in my case was cached the moment it was served up, meaning the flash message was also captured into that cache. So on the second time I clicked the create button, it would load the cached page with the flash, then redirect for real and flash again (like it's suppoed to).

    So the solution here is to either a. disable all page previews or b. disable turbolinks for that specific link. I chose b. because it won't affect the rest of my program, at the expense that the blue loading motion is not longer there. Here is the solution below (very simple):

    Before:

    <%= link_to create_wager_from_favorite_wager_path(fw, :anchor => anchor), data: { confirm: 'Create this wager?' }, class: "red-btn create-favorite-wager-btn" do %>
      Create Wager
    <% end %>

    After:

    <%= link_to create_wager_from_favorite_wager_path(fw, :anchor => anchor), data: { confirm: 'Create this wager?' }, "data-turbolinks": "false", class: "red-btn create-favorite-wager-btn" do %>
      Create Wager
    <% end %>