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:
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.