leonid-shevtsov / unobtrusive_flash

Turnkey Flash messages for your Rails app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flash not showing after redirect with Turbolinks

matissg opened this issue · comments

In my update action I have

respond_to do |format|
  if @offer.update(offer_params)
    flash[:success] = "#{@offer.name} #{t(:updated)}"
    format.js
  else
    format.js {render :edit}
  end
end

Then in update.js.erb redirect is done with:
Turbolinks.visit("<%= seller_offers_path %>");

In application.js there is

$(document).on('turbolinks:load', UnobtrusiveFlash.showFlashFromCookies, function() {
  //something...
});

As in this suggestion in session_store.rb I've set:
Rails.application.config.session_store :cookie_store, key: 'myapp', expire_after: 1.day, httponly: { except: ['flash'] }

At the moment redirect after update is done, however no flash. If I remove Turbolinks.visit("<%= seller_offers_path %>"); then flash is shown, but I stay in same page as there is no redirect.

Is there a way to have flash on next page after redirect with Turbolinks, please?

My tech stack: Rails 6, unobtrusive_flash 3.3.1, Ruby 2.6.3

This may be caused by turbo/turbolink replaced current page on flash show, this is the process:

request -> redirect with flash message -> show flash message but turbo/turbolink will replace current page immediately, so you wouldn't notice it.

You can find out this by remove UnobtrusiveFlash.showFlashFromCookies callback, and call UnobtrusiveFlash.showFlashFromCookies in chrome console after redirect.

You may solve this by show flash message after turbo/turbolink render

  $(document).on('turbo:render', function(){
    UnobtrusiveFlash.showFlashFromCookies()
  })