Rails flash message remains for two page loads
There are two ways to solve this problem:
- flash.now
- flash.discard
One is to use
flash.now[:notice]
when your flash must be discarded at the end of the current request and is not intended to be used after a redirect.
The second one is to call
flash.discard(:notice)
at the end of the request.
The standard flash message is intended to be kept for the "next" request. E.g. you generate a flash while handling a create or edit request, then redirect the user to the show screen. When the browser makes the next request to the show screen, the flash is displayed.
If you actually generate a flash on the show screen itself, use flash.now
.
Check the Ruby on Rails API documentation to see how the Flash hash works
flash message in Rails stays until next webpage
Use now
method:
flash.now[:alert] = 'Please fix mistakes outlined in red'
If link_to is clicked flash message on new page?
view
<%= link_to performance_path(alert: true), class: "btn", style: "padding-top: 10px; padding-bottom: 10px; margin-top: 15px; color: white;" do %>
<span class='glyphicon glyphicon-tower'></span> Duel
<% end %>
controller
def performance
@user = current_user
flash[:alert] = "YOU'RE NOT AUTHORIZED TO INITIATE A DUEL UNTIL YOU BECOME A NINJA, BUT YOU CAN BE INVITED TO A DUEL" if params[:alert].present?
end
performance view
<% if flash[:alert] %>
<%= flash[:alert] %>
<% end %>
rails: prevent flash message from showing twice
flash.now[:notice] = 'message'
Rails 4: Flash message persists for the next page view
Use flash.now
instead of flash
.
The flash
variable is intended to be used before a redirect
, and it persists on the resulting page for one request. This means that if we do not redirect
, and instead simply render
a page, the flash
message will persist for two requests: it appears on the rendered page but is still waiting for a redirect (i.e., a second request), and thus the message will appear again if you click a link.
To avoid this weird behavior, when rendering rather than redirecting we use flash.now
instead of flash
.
The flash.now
object is used for displaying flash
messages on a rendered page. As per my assumption, if you ever find a random flash
message where you do not expect it, you can resolve it by replacing flash
with flash.now
.
How to show the flash message in Rails only once?
This is probably a generic browser caching problem.
You can force your browser to reload a page while hitting the back button by setting some no-cache headers.
You could try this approach: (found after a quick google search, you might want to dig deeper)
http://blog.serendeputy.com/posts/how-to-prevent-browsers-from-caching-a-page-in-rails/
Related Topics
How to Get the Width of Terminal Window in Ruby
How to Load a Spec_Helper.Rb Automatically in Rspec 2
Postgresql Gem Pg Was Unable to Install
Neither Ruby and Nor Irb Can Load .Rb File in Current Directory
How to Delete All Contents of a Folder with Ruby-Rails
Gem File with Git Remote Failing on Heroku Push
Bundle Command Not Found in Linux Debian
How to Add a Method to the Global Scope in Ruby
How to Sort a Hash by Value in Descending Order and Output a Hash in Ruby
Rails Routing (Root :To => ...)
Renaming Uploaded Files with Carrierwave
Ruby: Get All Keys in a Hash (Including Sub Keys)
Cleanest Way to Create a Hash from an Array
How to Return Http 204 in a Rails Controller
Carrierwave: Create the Same, Unique Filename for All Versioned Files