Setting a "global" variable

hi, all…

i have an activerecord object called:

@site_info = SiteInfo.find(:first)

that is used on pretty much every page on my site…

i am finding that i have to set that all over the place in my app…

is it possible to put this declaration somewhere where i can find do:

@site_info.site_name

anywhere in my app?

i have tried putting it in application.rb, and at the top of all my
controllers, with no luck…

any help would be appreciated…

thanks!

Why not using global variable?
$site_info = SiteInfo.find(:first)

and all of your rails files can access $site_info.

Hi –

On Sat, 16 Dec 2006, Sergio R. wrote:

is it possible to put this declaration somewhere where i can find do:

@site_info.site_name

anywhere in my app?

i have tried putting it in application.rb, and at the top of all my
controllers, with no luck…

I suspect you’ve done something like this:

class ApplicationController < ActionController::Base

 @site_info = SiteInfo.find(:first)

and then in another controller:

 def do_something
   name = @site_info.site_name
 end
 ...

end

and gotten a NoMethodError for calling “site_name” on nil.

Am I close? :slight_smile:

If that’s the case, then the problem is that you’ve defined the
instance variable to be a property of the class object
ApplicationController – which, because of how instance variables work
in Ruby (each one belongs to exactly one object) means that it’s not
available where you really need it to be: namely, the controller
instance.

So, try this:

class ApplicationController < ACB
before_filter set_site_info
def set_site_info
@site_info = SiteInfo.find(:first)
end

 ...

end

That way, every controller instance will set the variable, and it will
be visible to the controller (and the view, thanks to some “magic”
behind the scenes) for the duration of the request.

David


Q. What’s a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (Ruby for Rails)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Hi –

On Tue, 19 Dec 2006, Sergio R. wrote:

 ...

NameError (undefined local variable or method `set_site_info’ for
ApplicationController:Class):

Whoops, I left the : off of :set_site_info. Sorry. It should be:

before_filter :set_site_info

David


Q. What’s a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (Ruby for Rails)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

So, try this:

class ApplicationController < ACB
before_filter set_site_info
def set_site_info
@site_info = SiteInfo.find(:first)
end

 ...

end

hi!

thanks so much for your help…

i tried doing in this way, but i am having no luck…

i am getting this error now:

NameError (undefined local variable or method `set_site_info’ for
ApplicationController:Class):

i am thinking about adding:

<%- @site_info = SiteInfo.find(:first) %>

into my layout… but i really don’t like doing business in the
template…

any ideas why i might be having a the above error?

thanks!

Whoops, I left the : off of :set_site_info. Sorry. It should be:

before_filter :set_site_info

hi, david!

thanks so much for your help!

btw…

i just ordered your book… based on your sig… so, if you are wondering
if it works, it does…

thanks again!

Funny - it went the other way for me: I purchased the book the learn
about ruby and rails, then find the author is quite active here. :slight_smile:
It’s one of the great things about Ruby and RoR that I hope we will
never lose.

On Dec 19, 2:58 pm, Sergio R. [email protected]