So I am trying to simply track the number of hits a link has received
and display that number on the site so other users can see which links
are more popular. So once the link is clicked on it adds a +1 value to
the hits column in the database and then redirects the person to the
url that the link is supposed to go to. So far this is what I have.
I have a tutorials table in my database and a column for hits and a
column for url.
** tutorials_controller.rb **
def update_hits_count @tutorial.update_attribute :hits, params[:count].size + 1
if @tutorial.save
redirect_to url_for(@tutorial.url)
end
end
** view/tutorials/show.html.erb **
<% for tutorial in @tutorials %>
<%=link_to tutorial.title, {:action => ‘update_hits_count’, :count
=> tutorial.hits} %>
<% end %>
That is what I have so far and when you click on the link the hits
column in the tutorials table just gets set to null and it does not
redirect them to the proper url. Any help would be greatly
appreciated. Thanks in advance.
def update_hits_count @tutorial.update_attribute :hits, params[:count].size + 1
if @tutorial.save
redirect_to url_for(@tutorial.url)
end
end
modify the method so it suits any controller,
like in self.update_attribute
add the method to application_controller
whenever you want to add a hit you call it,
like in
def index
update_hits_count
end
Thanks for the response, sorry I’m totally new to this any way you can
elaborate or show example? Any help would be greatly appreciated,
thanks in advance
Note that writing to the db every time something on your site is viewed
can
really limit your capacity and scale and hasten the day when you’ll need
to
think about multi-master configuration. So unless you’re using
something
like amazon SimpleDB, which scales for you, it might be better to figure
out
a better way to track view counting (e.g. central log server, cron job
to
aggregate and update db?).
So I am trying to simply track the number of hits a link has received
and display that number on the site so other users can see which links
are more popular.
Dare I mention that this method counts a link as popular even if the
user arrived at the destination page and considered it rubbish? (Not to
impune your content or anything).
A voting system where users actively choose to flag the content as
“good” or “popular” may be more useful and meaningful…
are more popular. So once the link is clicked on it adds a +1 value to
if @tutorial.save
update_hits_count
end
it really depends on what you want to track
hits on a single controller/action (probably writing on a file) or in
many
more likely you’ll need a tracking model “hits”
that has something like
controller :string
action :string
hits :integer
define a addhit method on the model
self.hits = self.hits +1
create a method in application controller
def update_hit_counts @hit.find(:first, :controller => controller, :method => method) @hit.addhit
(you can use the current method and controller form rails environmental
variables)
then when you want to register a hit
inside the method you call
update_hit_counts
def inc_link_count
li = Link.find(params[:ID])
li.count++
li.save
end
To avoid multiple connections stepping on one another, you should
consider using update_counters (or a custom method that does the same
thing) instead of doing a find, modifying, and saving.
Why don’t you pass an unique ID parameter in each link (either GET or
POST) and use this in a before_filter in the application controller?
The filter would just increment the count for that ID in a separate
table. This is a general design that would work for anything. Later
you can use this information with some sort of mapping that says what
each ID means (or just put a title for each ID in the table).
Something like:
Ar Chron is right, I should just put some sort of voting system in
place, guess I was trying to take the easy way out. Back to the
drawing board, thanks everyone for your help
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.