Hi, i try to figure out the solution for simple banner ad script. I''ve db table ''banners'' with fields ''id'', ''image'', and ''url''. I suppose to keep in this table 5 banners, so the ids are always from 1 to 5. I just wont to loop from 1 to 5 and show appropriate banner, in header layout (not view). At this time i put the banner into layout''s header with direct query to db using Banner model: *banner.rb* class Banner < ActiveRecord::Base def self.banner @banner = Banner.find(:first, :conditions => [''date_end > ?'', Time.now.strftime("%Y-%m-%d %H:%M:%S")]) @banner.nil? ? "adimage.gif" : @banner.image end *layout.rhtml* <div id="headerbanner"> <%= image_tag(Banner.banner) %> </div> I think i must increment some variable in layout.rhtml and return this to Banner model. Or there is a better way? 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 -~----------~----~----~----~------~----~------~--~---
Craig Beck
2007-Jan-21 17:43 UTC
Re: simple banner / ad rotation on page (newbie question)
On Jan 21, 2007, at 4:30 AM, ogurec wrote:> > Hi, > i try to figure out the solution for simple banner ad script. I''ve db > table ''banners'' with fields ''id'', ''image'', and ''url''. I suppose to > keep > in this table 5 banners, so the ids are always from 1 to 5. > I just wont to loop from 1 to 5 and show appropriate banner, in header > layout (not view). > > At this time i put the banner into layout''s header with direct > query to > db using Banner model: > > *banner.rb* > class Banner < ActiveRecord::Base > def self.banner > @banner = Banner.find(:first, :conditions => [''date_end > ?'', > Time.now.strftime("%Y-%m-%d %H:%M:%S")]) > @banner.nil? ? "adimage.gif" : @banner.image > end > > *layout.rhtml* > <div id="headerbanner"> > <%= image_tag(Banner.banner) %> > </div> > > I think i must increment some variable in layout.rhtml and return this > to Banner model. > Or there is a better way?How about having your layout call a banner_controller that handles the logic of picking which banner to present to the user? That seems like a more natural place to keep the logic, and a far better place to extend from later (view tracking, click tracking, whatever...) -- Craig Beck AIM: kreiggers --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Can you give me some advice, how to do this? --~--~---------~--~----~------------~-------~--~----~ 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, > > i try to figure out the solution for simplebannerad script. I''ve db > > table ''banners'' with fields ''id'', ''image'', and ''url''. I suppose to > > keep > > in this table 5 banners, so the ids are always from 1 to 5. > > I just wont to loop from 1 to 5 and show appropriatebanner, in header > > layout (not view). > > > At this time i put thebannerinto layout''s header with direct > > query to > > db usingBannermodel: > > > *banner.rb* > > classBanner< ActiveRecord::Base > > def self.banner > > @banner=Banner.find(:first, :conditions => [''date_end > ?'', > > Time.now.strftime("%Y-%m-%d %H:%M:%S")]) > > @banner.nil? ? "adimage.gif" : @banner.image > > end > > > *layout.rhtml* > > <div id="headerbanner"> > > <%= image_tag(Banner.banner) %> > > </div> > > > I think i must increment some variable in layout.rhtml and return this > > toBannermodel. > > Or there is a better way?How about having your layout call a banner_controller that handles > the logic of picking whichbannerto present to the user? > > That seems like a more natural place to keep the logic, and a far > better place to extend from later (view tracking, click tracking, > whatever...) > -- > Craig Beck > > AIM: kreiggersIn my layout i call the method ''banner'' of BannerController class: <%= image_tag(BannersController.banner) %> But how the controller "remembers" which banner is currently shown to user ? How or where is possible to store the id of last banner? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
any suggestion? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter Ertl
2007-Jan-25 16:35 UTC
Re: simple banner / ad rotation on page (newbie question)
define / require this in your environment.rb (or in lib or somewhere else): class Counter @@counter = 0 def self.nextValue @@counter = @@counter + 1 end end in your action do this: def my_action @counter = Counter.nextValue end now you can access the current counter value in your view <%= @counter %> This is sufficient for rotating banners *imho* 2007/1/25, ogurec <moto-IuykoLEpJBA@public.gmane.org>:> > > any suggestion? > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Andreas Schwarz
2007-Jan-25 17:31 UTC
Re: simple banner / ad rotation on page (newbie question)
Ilja Ivanov wrote:> I think i must increment some variable in layout.rhtml and return this > to Banner model. > Or there is a better way?Just use rand() to select the banner to show. If you want different weights, there''s WeightedSelection (http://www.gemjack.com/gems/WeightedSelection-1.0.0/). -- 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 -~----------~----~----~----~------~----~------~--~---
Alain Ravet
2007-Jan-27 21:38 UTC
Re: simple banner / ad rotation on page (newbie question)
If you use MySql (not sure RAND()) works with other DB) : In your model ============class Banner < ActiveRecord::Base ... class << self def random(limit=1) find(:all, :order => ''RAND()'', :limit => limit) end end end In your view/controller ================ @banners = Banner.random(3) Alain --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
First I assumed, that the quantity of images in table will be constant. But later the problem has become complicated ( for me as newbie :) ), because I''ve added the field ''date_end'', so that banner can expire. It means, that the quantity of images varies, and a very simple looping through id in table does not give the solution. Because as result of executing code @banners = Banner.find(@counter, :conditions => [''date_end > ?'', Time.now.strftime("%Y-%m-%d %H:%M:%S")]) i has error like ''Could not find record with id = X and Date_end > Today'' I tried @banners.nil? and similar statements (blank, empty), but with no success. I think, this is because the error occures before the @banners became some value or nil at all. As my first post says, banners table has fields ''id'', ''image'', and ''url''. At this time process works this way: in environment.rb i''ve added the Counter class as Peter Ertl suggested. class Counter @@counter = 0 def self.increment_cnt @@counter += 1 end def self.toone_cnt @@counter = 1 end end in controller that manages banners: def self.banner @counter = Counter.increment_cnt @banners = Banner.find(:all, :conditions => [''date_end > ?'', Time.now.strftime("%Y-%m-%d %H:%M: %S")]) if @counter > @banners.size then @counter = 1 Counter.toone_cnt end [[@banners[@counter-1].image] , [@banners[@counter-1].url]] end in layout <% banner = BannersController.banner %> <%= link_to image_tag(banner[0].to_s ), banner[1].to_s %> So, this way i do not loop through ids of records in tables, but through elements of @banners array. Elements are in fact ActiveRecord objects, it doesn''t matter what ids they had --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---