I''d like to display a banner on every page. Ultimately I''ll be tweaking which banner is displayed via different controllers but initially I''d just like to set a single banner application wide and display it in my "_header", a partial I include in all layouts. I''ve got a BannerAdvert model, simple. If I''m in my welcome controller index action I can simply do, @banner_url = BannerAdvert.first.url in my _header partial (view) I display the url, works fine. ----- BUT - my header is called from other controllers, I don''t want to be non-dry and repeat @banner_url = BannerAdvert.first.url all over the place. I''d like to set @banner_url = BannerAdvert.first.url application wide for now - how do I do this, should be easy but I''m having a Friday afternoon mental block! I thought application_controller and application_helper, but putting the code in either of those two didn''t work. Please help cure my insanity, any inspiration really appreciated. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> </head> <body bgcolor="#ffffff" text="#000000"> bingo bob wrote: <blockquote cite="mid:21cc72a800f804b1a4197de9fecf7cf6-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org" type="cite"> <pre wrap="">I''d like to display a banner on every page. Ultimately I''ll be tweaking which banner is displayed via different controllers but initially I''d just like to set a single banner application wide and display it in my "_header", a partial I include in all layouts. I''ve got a BannerAdvert model, simple. If I''m in my welcome controller index action I can simply do, @banner_url = BannerAdvert.first.url in my _header partial (view) I display the url, works fine. ----- BUT - my header is called from other controllers, I don''t want to be non-dry and repeat @banner_url = BannerAdvert.first.url all over the place. I''d like to set @banner_url = BannerAdvert.first.url application wide for now - how do I do this, should be easy but I''m having a Friday afternoon mental block! I thought application_controller and application_helper, but putting the code in either of those two didn''t work. Please help cure my insanity, any inspiration really appreciated. </pre> </blockquote> <font face="Helvetica, Arial, sans-serif" size="-1">You could put the @banner_url = BannerAdvert.. in you application_controller.rb. Make it a before_filter and then display it in your layout.<br> <br> <br> </font> </body> </html> <p></p> -- <br /> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.<br /> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org<br /> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org<br /> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.<br />
Aha ! "You could put the @banner_url = BannerAdvert.. in you application_controller.rb. Make it a before_filter." That''s the bit I missed then, I just stuck @banner_url = BannerAdvert.first in the application_controller.rb, I need a before_filter to call a method in that controller - that right ? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
bingo bob wrote:> > Aha ! > > "You could put the @banner_url = BannerAdvert.. in you > application_controller.rb. Make it a before_filter." > > That''s the bit I missed then, I just stuck @banner_url = > BannerAdvert.first in the application_controller.rb, I need a > before_filter to call a method in that controller - that right ?No; you could call it explicitly every time. But you usually wouldn''t want to. :) Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Making it a before_filter makes it so you won''t need to call the method to set your banner every single time. It will be done before any action is called. On Jul 2, 12:25 pm, bingo bob <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Aha ! > > "You could put the @banner_url = BannerAdvert.. in you > application_controller.rb. Make it a before_filter." > > That''s the bit I missed then, I just stuck @banner_url > BannerAdvert.first in the application_controller.rb, I need a > before_filter to call a method in that controller - that right ? > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 to all, settled on this code in the end in case useful to anyone else - further tips appreciated, works great though. class ApplicationController < ActionController::Base before_filter :grab_banner def grab_banner banner = BannerAdvert.first(:conditions => [ "published >= ?", 1], :order => "RAND()") if banner @banner_url = banner.url end end end I realised "lightening bolt", that for many thing I don''t actually NEED an admin interface/scaffold - like in this case I just created a BannerAdvert model and I CRUD the records directly with Sequel Pro. Anyone comment on this approach - saves me hours and keeps the app clean. Silly, simple tip - no more scaffolded code - except when it helps me - feels like coming on to the next stage.. :-). -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2010-Jul-03 14:54 UTC
Re: Re: making something available application wide
On Sat, Jul 3, 2010 at 3:30 AM, bingo bob <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > Thanks to all, settled on this code in the end in case useful to anyone > else - further tips appreciated> banner = BannerAdvert.first(:conditions => [ "published >= ?", 1], > :order => "RAND()")Even if you''re currently only working with a single database, it might be a good idea to abstract that last statement. Different DBs use a different syntax, so for example I''d write that as :order => RANDOM where RANDOM is set in an initializer based on the DB actually being used, e.g. <http://pastie.org/1029327> I work in a lot of "mixed" environments, so that may be more useful to me than most people, but... FWIW, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.