Hello, 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, 2008-09-25 at 11:35 -0700, Dan Paul wrote:> Hello, > > 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 > endmodify 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> > ** 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. > > --~--~---------~--~----~------------~-------~--~----~ > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > -~----------~----~----~----~------~----~------~--~--- >--
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 On Sep 25, 11:53 am, andres <and...-pMggkaTgBv1Wk0Htik3J/w@public.gmane.org> wrote:> On Thu, 2008-09-25 at 11:35 -0700, Dan Paul wrote: > > Hello, > > > 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 > > -319zWutJ0MFcQ7ALCyhmPw@public.gmane.org_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 > > > > > > > ** 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. > > > > > -- > > smime.p7s > 7KViewDownload--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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?). m On Thu, Sep 25, 2008 at 1:52 PM, Dan Paul <danpaul01-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > On Sep 25, 11:53 am, andres <and...-pMggkaTgBv1Wk0Htik3J/w@public.gmane.org> wrote: > > On Thu, 2008-09-25 at 11:35 -0700, Dan Paul wrote: > > > Hello, > > > > > 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 > > > > 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 > > > > > > > > > > > > > ** 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. > > > > > > > > -- > > > > smime.p7s > > 7KViewDownload > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, 2008-09-25 at 13:52 -0700, Dan Paul wrote:> 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 > > On Sep 25, 11:53 am, andres <and...-pMggkaTgBv1Wk0Htik3J/w@public.gmane.org> wrote: > > On Thu, 2008-09-25 at 11:35 -0700, Dan Paul wrote: > > > Hello, > > > > > 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 > > > > 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 > >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 (ads one query and one write per request)> > > > > > > > > > > ** 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. > > > > > > > > -- > > > > smime.p7s > > 7KViewDownload > > --~--~---------~--~----~------------~-------~--~----~ > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > -~----------~----~----~----~------~----~------~--~--- >-- Andres Paglayan CTO, StoneSoup LLC Ph: 505 629-4344 Mb: 505 690-2871 FWD: 65-5587 Testi. Codi. Vinci.
Hey Dan, 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: == Links <a href="http://mysite.com?ID=1">Link 1</a> == application.rb before_filter :inc_link_count def inc_link_count li = Link.find(params[:ID]) li.count++ li.save end Cheers, Sazima On Sep 25, 7:45 pm, andres <and...-pMggkaTgBv1Wk0Htik3J/w@public.gmane.org> wrote:> On Thu, 2008-09-25 at 13:52 -0700, Dan Paul wrote: > > 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 > > > On Sep 25, 11:53 am, andres <and...-pMggkaTgBv1Wk0Htik3J/w@public.gmane.org> wrote: > > > On Thu, 2008-09-25 at 11:35 -0700, Dan Paul wrote: > > > > Hello, > > > > > 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 > > > > -319zWutJ0MFcQ7ALCyhmPw@public.gmane.org_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 > > 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 > > (ads one query and one write per request) > > > > > > > > > ** 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. > > > > -- > > > > smime.p7s > > > 7KViewDownload > > > > > -- > Andres Paglayan > CTO, StoneSoup LLC > Ph: 505 629-4344 > Mb: 505 690-2871 > FWD: 65-5587 > Testi. Codi. Vinci. > > smime.p7s > 7KViewDownload--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dan Paul wrote:> Hello, > > 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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> def inc_link_count > li = Link.find(params[:ID]) > li.count++ > li.save > endTo 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. Peace. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Sep 26, 9:08 am, Ar Chron <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Dan Paul wrote: > > Hello, > > > 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... > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---