0

I have a very "basic" issue, simple to explain but hard to solve.

On my homepage, I show a user the number of times he saw the page we can call Alpha.

<div>
<%= link_to "page alpha",
            page_alpha_path(deal) %> <br/>
You have seen this page alpha #{deal.number_of_times_viewed} times
</div>

It works. but I 'd like to be able to do the following for example:

  • user goes on homepage and sees the link and can read "You have seen this page alpha 4 times times already"
  • he clicks on the alpha page link and arrive and alpha page
  • he presses Chrome's 'Back button' and sees now 'You have seen this page alpha 5 times

As you see even on 'press back' the page has reloaded the number of times, it has triggered a call to the database table to recheck the number of times he saw the page.

Today unfortunately, whatever I tried, he still sees "4 times"

How to do it? Can I do it in pure Ruby/Rails or might I need some javascript or gon Watch ? or maybe some Rails UJS?

Mathieu
  • 4,587
  • 11
  • 57
  • 112
  • follow this links may help you http://stackoverflow.com/questions/36188063/refresh-all-information-on-back-button-press-ruby-on-rails http://stackoverflow.com/questions/711418/how-to-prevent-browser-page-caching-in-rails – Narasimha Reddy - Geeker May 03 '16 at 10:09
  • @NarasimhaReddy thanks, I saw it but there was no "acceptance" of the proposed answers so I think their suggestions did not work – Mathieu May 03 '16 at 10:12

2 Answers2

1

try to clear your response cache like this using a call_back. it will work.

class ApplicationController < ActionController::Base

    before_filter :set_cache_headers

      private

      def set_cache_headers
        response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
        response.headers["Pragma"] = "no-cache"
        response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
      end
    end
Narasimha Reddy - Geeker
  • 3,510
  • 2
  • 18
  • 23
  • I'll try this week end. the problem is here I will bust all the cache whereas in my question I ONLY want to update one little tiny part of the page, the {real updated} number of views – Mathieu May 05 '16 at 15:47
0

Try using this code:

class ApplicationController < ActionController::Base
before_filter :pages_count

def pages_count
  if current_user
   current_user.update_attributes(:pages_count=>current_user.pages_count+ 1)
   end
end
kajal ojha
  • 1,248
  • 8
  • 20