Leonel *.*
2010-Oct-18 16:24 UTC
Application Controller instance variable available to all views
Currently, I have this on the application controller... def company_name User.find_by_id(session[:user_id]).account.name end So then in each controller I have to add this, which is not very DRY. The @company_id needs to be used in almost every page and it''s accessed on the application.html.erb on a link @company_id = company_id (has to be added to EVERY CONTROLLER) How can I NOT have to add the above line in every controller? Is there a way to add it to the application controller and have it available to application.html.erb regardless on what page im on? -- 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.
Marnen Laibow-Koser
2010-Oct-18 16:44 UTC
Re: Application Controller instance variable available to all views
Leonel *.* wrote in post #955167:> Currently, I have this on the application controller... > def company_name > User.find_by_id(session[:user_id]).account.name > end > > So then in each controller I have to add this, which is not very DRY. > The @company_id needs to be used in almost every page and it''s accessed > on the application.html.erb on a link > @company_id = company_id (has to be added to EVERY CONTROLLER) > > How can I NOT have to add the above line in every controller? Is there a > way to add it to the application controller and have it available to > application.html.erb regardless on what page im on?You asked a nearly identical question a few days ago. Please read http://groups.google.com/group/rubyonrails-talk/msg/c8a1cc193e362316 again, where I answered this exact question. 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.
Colin Law
2010-Oct-18 16:49 UTC
Re: Application Controller instance variable available to all views
On 18 October 2010 17:24, Leonel *.* <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Currently, I have this on the application controller... > def company_name > User.find_by_id(session[:user_id]).account.name > end > > So then in each controller I have to add this, which is not very DRY. > The @company_id needs to be used in almost every page and it''s accessed > on the application.html.erb on a link > @company_id = company_id (has to be added to EVERY CONTROLLER) > > How can I NOT have to add the above line in every controller? Is there a > way to add it to the application controller and have it available to > application.html.erb regardless on what page im on?A before_filter is not restricted to use with a regular controller, it can also be used with the application_controller. Colin> > -- > 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@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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.