# Making something available application wide

**URL:** <https://www.ruby-forum.com/t/making-something-available-application-wide/191011>\
**Category:** Rails\
**Created:** [July 2, 2010, 4:55pm UTC](https://www.ruby-forum.com/t/making-something-available-application-wide/191011 "2010-07-02T16:55:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 2, 2010, 4:55pm UTC](https://www.ruby-forum.com/t/making-something-available-application-wide/191011/1 "2010-07-02T16:55:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 2, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/making-something-available-application-wide/191011/2 "2010-07-02T17:25:03Z")

</div>

bingo bob wrote:

> ```
> 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.
> ```

You could put the @banner\_url = BannerAdvert.. in you application\_controller.rb.&nbsp; Make it a before\_filter and then display it in your layout.  
  
  

–   
  
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  
removed\_email\_address@domain.invalid.  
  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.

For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en.](http://groups.google.com/group/rubyonrails-talk?hl=en.)

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 2, 2010, 6:25pm UTC](https://www.ruby-forum.com/t/making-something-available-application-wide/191011/3 "2010-07-02T18:25:17Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 2, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/making-something-available-application-wide/191011/4 "2010-07-02T20:27:01Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 3, 2010, 12:21pm UTC](https://www.ruby-forum.com/t/making-something-available-application-wide/191011/5 "2010-07-03T12:21:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 3, 2010, 12:30pm UTC](https://www.ruby-forum.com/t/making-something-available-application-wide/191011/6 "2010-07-03T12:30:31Z")

</div>

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… :-).

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 3, 2010, 4:55pm UTC](https://www.ruby-forum.com/t/making-something-available-application-wide/191011/7 "2010-07-03T16:55:46Z")

</div>

On Sat, Jul 3, 2010 at 3:30 AM, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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 S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan
