Disabling Flash Message Without Disabling Cache on Click on Back Button in Rails

Disabling flash[:notice] to make page caching make sense

Look up dynamic page caching. Uses Javascript/Ajax to update signed in messages:

http://asciicasts.com/episodes/169-dynamic-page-caching

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/

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

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 %>

How to prevent browser page caching in Rails

I finally figured this out - http://blog.serendeputy.com/posts/how-to-prevent-browsers-from-caching-a-page-in-rails/ in application_controller.rb.

After Ruby on Rails 5:

class ApplicationController < ActionController::Base

before_action :set_cache_headers

private

def set_cache_headers
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Mon, 01 Jan 1990 00:00:00 GMT"
end
end

Ruby on Rails 4 and older versions:

class ApplicationController < ActionController::Base

before_filter :set_cache_headers

private

def set_cache_headers
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Mon, 01 Jan 1990 00:00:00 GMT"
end
end

Rails: Prevent duplicate inserts due to pressing back button and save again

After some investigations I found a suitable solution based on cookies. Here it is:

In the controller's "new" action, a timestamp with the current time is generated and rendered in the form as hidden field. When the user submits the form, this timestamps gets back to the controller's "create" action. After creating the record, this timestamp is stored in the session cookie. If the user goes back to the "new" form via browser's back button, he gets a stale form, which means its timestamp is older than the one stored in the cookie. This is checked before creating the record and results in an error message.

Here is the controller code:

def new
@post = Post.new
@stale_form_check_timestamp = Time.now.to_i
end

def create
@post = Post.new(params[:post])

if session[:last_created_at].to_i > params[:timestamp].to_i
flash[:error] = 'This form is stale!'
render 'new'
else
@post.save!
@stale_form_check_timestamp = Time.now.to_i
session[:last_created_at] = @stale_form_check_timestamp
end
end

And here the form code:

- form_for @post do |f|
= tag :input, :type => 'hidden', :name => 'timestamp', :value => @stale_form_check_timestamp
= f.input :some_field
= .......


Related Topics



Leave a reply



Submit