I have <%= simple_form_for @offer, remote: true %>
with Turbolinks enabled. I'm trying to redirect and show success flash with something like this in my update
action:
respond_to do |format|
if @offer.update(offer_params)
flash[:success] = "#{@offer.name} #{t(:updated)}"
format.html { redirect_to seller_offers_path }
format.js
else
format.html { render :edit }
end
end
My update.js.erb
has code not related to redirect or flash:
$(".ibox-content.actions").removeClass('sk-loading');
$('.sk-spinner').css("display", "none");
In application.html.erb I can render flash with this:
<div class="row">
<% flash.each do |message_type, message| %>
<%= content_tag(:div, content_tag(:strong, message), id: "flash_message",
class: "text-bold alert alert-#{message_type}") %>
<% end %>
</div>
At the moment my form is saved, however no redirect.
When I add Turbolinks.visit(<%= seller_offers_path %>)
in update.js.erb
form is saved, redirect is done, however there is no flash.
Is there a way I can have flash shown on redirected page with Turbolinks and remote: true
, please? So far I've tried this suggestion, however it works only for showing flash on same page - it doesn't work with redirect_to
.
Update
This does redirect, however no flash:
respond_to do |format|
if @offer.update(offer_params)
format.js { redirect_to seller_offers_path, success: "#{@offer.name} #{t(:updated)}" }
else
format.html { render :edit }
end
end
Update 2
At the moment I'm trying to implement something like
Turbolinks.visit(url, { keep: 'flash' });
as mentioned in docs, however doesn't seem to be working with Turbolinks 5.2
Update 3
I've removerd update.js.erb
and in my controller I have
def update
if @offer.update(offer_params)
flash[:success] = "#{@offer.name} #{t(:updated)}"
redirect_to seller_offers_path
else
render :edit
end
end
Redirect is done as expected, however still no flash displayed on index
. I can print my flash at the end of update
action:
#<ActionDispatch::Flash::FlashHash:0x00007f180d2a1c60 @discard=#<Set: {}>, @flashes={"success"=>"My offer updated!"}, @now=nil>
however at the end of index
action (where I redirect_to) it is empty:
#<ActionDispatch::Flash::FlashHash:0x00007f180eb3b780 @discard=#<Set: {}>, @flashes={}, @now=nil>
Looks like my flash is not kept for next request. Is it because of Turbolinks, or other some reason? I'd be happy for any hint.