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 <[EMAIL PROTECTED]> 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 [email protected]
> To unsubscribe from this group, send email to [EMAIL PROTECTED]
> 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.
smime.p7s
Description: S/MIME cryptographic signature

