Tracking Views in Rails
ActsAsViewable is plugin that allows you to track page and asset views in your Rails application. For example, you can use it to track how many times a page is visited or how many times a particular image is viewed.
Trac: http://trac.intridea.com/trac/public/wiki/ActsAsViewable Subversion repository: http://svn.intridea.com/svn/public/acts_as_viewable
Installation:
1 |
script/plugin install http://svn.intridea.com/svn/public/acts_as_viewable |
OR
1 |
cd vendor/plugins svn co http://svn.intridea.com/svn/public/acts_as_viewable |
Create the tables where views will be tracked:
1 |
class CreateViewings 0 t.column :created_at, :datetime, :null => false t.column :updated_at, :datetime end end def self.down drop_table :viewings end end |
Set the objects you want to track views for:
1 |
class SomeAsset <p>Now you can increment views for these objects wherever you need to. For example in the show action of our SomeAssetController:</p> <pre name="code" class="ruby"> class SomeAssetController <p>To get the number of views:</p> <pre name="code" class="ruby"> @some_asset.views |