How to create a 'page counter' in a controller in rails

Rixcy

I have a Post model, and I'm wanting to implement a way of counting the amount of times a post has been visited through the posts_controller so that I can eventually order posts by view count. So far I've created a migration to add a 'view count' column to by Post model:

class AddViewcountToPosts < ActiveRecord::Migration
  def change
    add_column :posts, :view_count, :integer
  end
end

I basically want something in the PostsController show method to add one on to the view_count column every time the show action is accessed.

Any help would be greatly appreciated :)

Terry Raimondo

You can just increase this counter in your show action.

def show
  # ...
  @post.increment!(:view_count)
end

No magic here :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to create page visiter counter in codeigniter?

From Dev

How to create Page counter with shuffled links

From Dev

How to create a splash page for a split view controller

From Dev

Create visitor counter for each page

From Dev

How to create an array with counter?

From Dev

Adding a counter to controller. Rails 4

From Dev

How to create a second create method in Ruby on Rails controller?

From Dev

how does rails route to a page when there is no action in a controller

From Dev

how does rails route to a page when there is no action in a controller

From Dev

How to create a fake data for testing my ruby on rails controller

From Dev

How can I create a controller for polymorphic association using ruby on rails?

From Dev

How to test Controller post :create of JSON api on rails using rspec?

From Dev

Setting default page for Controller in Rails

From Dev

How to create a counter using jQuery

From Dev

How to create vowel counter in python

From Dev

How to create try counter in C?

From Dev

How to create a PHP login counter

From Dev

How to create try counter in C?

From Dev

How to create a counter in face detection?

From Dev

Create bare rails controller class

From Dev

Dynamically create methods in Controller (RAILS)

From Dev

Passing Parameters to Create Controller Rails

From Dev

Ruby on Rails: How to link/route from one view/page to another view/page with a different controller

From Dev

How to create more than one index page in Ruby on Rails

From Dev

How to create simple refresh page link with Ruby on Rails

From Dev

Rails: How to increment counter_cache?

From Dev

Ruby on Rails, How to reset a counter every day?

From Dev

Check current page from controller in Rails?

From Dev

Rails: Which controller is my page using?

Related Related

  1. 1

    how to create page visiter counter in codeigniter?

  2. 2

    How to create Page counter with shuffled links

  3. 3

    How to create a splash page for a split view controller

  4. 4

    Create visitor counter for each page

  5. 5

    How to create an array with counter?

  6. 6

    Adding a counter to controller. Rails 4

  7. 7

    How to create a second create method in Ruby on Rails controller?

  8. 8

    how does rails route to a page when there is no action in a controller

  9. 9

    how does rails route to a page when there is no action in a controller

  10. 10

    How to create a fake data for testing my ruby on rails controller

  11. 11

    How can I create a controller for polymorphic association using ruby on rails?

  12. 12

    How to test Controller post :create of JSON api on rails using rspec?

  13. 13

    Setting default page for Controller in Rails

  14. 14

    How to create a counter using jQuery

  15. 15

    How to create vowel counter in python

  16. 16

    How to create try counter in C?

  17. 17

    How to create a PHP login counter

  18. 18

    How to create try counter in C?

  19. 19

    How to create a counter in face detection?

  20. 20

    Create bare rails controller class

  21. 21

    Dynamically create methods in Controller (RAILS)

  22. 22

    Passing Parameters to Create Controller Rails

  23. 23

    Ruby on Rails: How to link/route from one view/page to another view/page with a different controller

  24. 24

    How to create more than one index page in Ruby on Rails

  25. 25

    How to create simple refresh page link with Ruby on Rails

  26. 26

    Rails: How to increment counter_cache?

  27. 27

    Ruby on Rails, How to reset a counter every day?

  28. 28

    Check current page from controller in Rails?

  29. 29

    Rails: Which controller is my page using?

HotTag

Archive