Mathieu
Mathieu

Reputation: 4787

Rails 4 - Update a specific data when user press browser back button

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:

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?

Upvotes: 0

Views: 131

Answers (2)

kajal ojha
kajal ojha

Reputation: 1278

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

Upvotes: 0

Narasimha Reddy - Geeker
Narasimha Reddy - Geeker

Reputation: 3860

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

Upvotes: 1

Related Questions