Post visit count


#1

I have blog and I want to count number of visits made on each post. I
want to store the count in the database.

How do i do that.

I am new to rails.

Thank you,
Raj.


#2

I would say first off add a view_count field to your migration for the
post model
and then add an increment_view_count method to the post model.

You can then call that method from within the view method of your post
controller.

Something like that anyway.

Keith


#3

blog controller
def show
@post = Post.find(params[:id])
increment_count

end

in post model

def increment_count
count += 1
end

model has count field.

I am getting this error

undefined local variable or method `increment_count’


#4

it works without error but doesnt update the table count value.


#5

Try this

def show
@post = Post.find(params[:id])
@post.increment_count
@post.save
end

def increment_count
unless count.nil?
count += 1
else
count = 1
end
end


#6

On 5/21/07, raj removed_email_address@domain.invalid wrote:

it works without error but doesnt update the table count value.

@post.save


Greg D.
http://destiney.com/


#7

try this:

def increment_count
unless self.count.nil?
self.count += 1
else
self.count = 1
end
end


#8

thanks a lot tahts work.


#9

it doesnt work…

NoMethodError in BlogController#show

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.+

RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/models/post.rb:12:in increment_count' #{RAILS_ROOT}/app/controllers/blog_controller.rb:23:inshow’
-e:4:in `load’
-e:4

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:in
send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:inperform_action_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:632:in
call_filter' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:634:incall_filter’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:638:in
call_filter' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:438:incall’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:637:in
call_filter' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:619:inperform_action_without_benchmark’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in
perform_action_without_rescue' c:/ruby/lib/ruby/1.8/benchmark.rb:293:inmeasure’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in
perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/rescue.rb:83:inperform_action’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in
send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:inprocess_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:624:in
process_without_session_management_support' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/session_management.rb:114:inprocess’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:330:in
process' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/dispatcher.rb:41:indispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/webrick_server.rb:113:in
handle_dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/webrick_server.rb:79:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:162:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:95:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in each' c:/ruby/lib/ruby/1.8/webrick/server.rb:92:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:82:instart’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/webrick_server.rb:63:in
dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/servers/webrick.rb:59 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:inrequire’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in
new_constants_in' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:inrequire’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/server.rb:39
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
script/server:3

#{RAILS_ROOT}/app/models/post.rb:12:in increment_count' #{RAILS_ROOT}/app/controllers/blog_controller.rb:23:inshow’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:in
send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:inperform_action_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:632:in
call_filter' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:634:incall_filter’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:638:in
call_filter' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:438:incall’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:637:in
call_filter' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:619:inperform_action_without_benchmark’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in
perform_action_without_rescue' c:/ruby/lib/ruby/1.8/benchmark.rb:293:inmeasure’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in
perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/rescue.rb:83:inperform_action’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in
send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:inprocess_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:624:in
process_without_session_management_support' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/session_management.rb:114:inprocess’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:330:in
process' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/dispatcher.rb:41:indispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/webrick_server.rb:113:in
handle_dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/webrick_server.rb:79:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:162:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:95:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in each' c:/ruby/lib/ruby/1.8/webrick/server.rb:92:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:82:instart’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/webrick_server.rb:63:in
dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/servers/webrick.rb:59 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:inrequire’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in
new_constants_in' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:inrequire’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/server.rb:39
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
script/server:3
-e:4:in `load’
-e:4

Request

i have that error