I have the following command on a controller def increasecount Item.update (1, {:count=>''10''} ) end I am able to call this in a view ''increasecount.rhtml'' I need to do the following: Increase the count when a link is accessed in the application Things which I tried and does not work: def increasecount Item.update (1, {:count=>count+1} ) end Is it possible to make a method of this and then call it from other views? -- 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 -~----------~----~----~----~------~----~------~--~---
On 2/11/07, Ranjith Venkatesh <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I need to do the following: > Increase the count when a link is accessed in the application > > Things which I tried and does not work: > def increasecount > Item.update (1, {:count=>count+1} ) > end > > Is it possible to make a method of this and then call it from other > views?Active Record provides you with Item.increment_counter :count, 1234 which performs ''update items set count=count+1 where id=1234'' You may wrap that in an instance method such as class Item < AR::Base def increment_count! self.class.increment_count :count, id end end Then call @item.increment_count! as you please. jeremy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi jeremy, Thanks for the tip! I have a html page which was calling a javascript. Here is the snippet below: <div style="float:left;margin-left:10px;"> <a id=''Choice1Image'' href="javascript:IncrementCount(id);" onfocus="toggleImage(this.id)" onblur="toggleImageBack(this.id)"> <img name=''button'' alt=''Choice1'' src="/images/choice1.gif"/> </a> </div> I need to call the increment when the user click this link. My Choice class looks like this now: class Choice < ActiveRecord::Base belongs_to:Question def increment_count! self.class.increment_counter :count, id end end Where do I substitute the javascript function call with the ruby method call? -- 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 -~----------~----~----~----~------~----~------~--~---