Tutorial :How to show Number of Views (like This link/page has been viewed 68 times) in rails?



Question:

How to show Number of Views (like : This link/page has been viewed 68 times) in rails? I sthere any gem or plugin available for it ?


Solution:1

If you are following the usual MVC pattern, then a View is a representation of the object model and the best way to track a view count would be to simply add a 'number of times viewed' counter to the table.


Solution:2

Simply add a counter column to your Model, and increment it whenever the page is hit in the controller. As simple as this is, there are some design issues:

  • Page caching will affect your stats. In this case, you can still use Ajax on your page to register the hit. (Not much different than how google analytics works.)

  • Do your pages already have an obvious relationship to a Model? If they are user profile pages for example, put the counter in the Profile model.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »