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! -- 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 -~----------~----~----~----~------~----~------~--~---
Why not using global variable? $site_info = SiteInfo.find(:first) and all of your rails files can access $site_info. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-16 14:39 UTC
Re: setting a "global" variable..
Hi -- On Sat, 16 Dec 2006, Sergio Ruiz wrote:> > 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..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? :-) 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 (http://www.manning.com/black) 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) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> 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! -- 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-19 03:11 UTC
Re: setting a "global" variable..
Hi -- On Tue, 19 Dec 2006, Sergio Ruiz wrote:> > >> 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):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 (http://www.manning.com/black) 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) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> 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! -- 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 -~----------~----~----~----~------~----~------~--~---
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. :) 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 Ruiz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Whoops, I left the : off of :set_site_info. Sorry. It should be: > > > before_filter :set_site_infohi, 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! > > -- > 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-/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 -~----------~----~----~----~------~----~------~--~---