Jeff Ward
2006-Jun-02 01:21 UTC
[Rails] Set instance variable for all actions in the Controller
Can I set an application wide instance variable that is available for all actions...and their views? eg. class ApplicationController < ActionController::Base @current_user = User.find(session[:user_id]) end and everywhere I can call @current_user.id and I can get that object? Even down in the views? I could not get this to work... Thanks in advance, Jeff -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060602/0a8eb130/attachment.html
dblack@wobblini.net
2006-Jun-02 01:29 UTC
[Rails] Set instance variable for all actions in the Controller
Hi -- On Thu, 1 Jun 2006, Jeff Ward wrote:> Can I set an application wide instance variable that is available for all > actions...and their views? > > eg. > class ApplicationController < ActionController::Base > @current_user = User.find(session[:user_id]) > end > > and everywhere I can call @current_user.id and I can get that object? Even > down in the views? > > I could not get this to work...What you''ve got there is an instance variable, but it''s an instance variable that belongs to the class object, ApplicationController, itself. Like so many things in Ruby, it comes down to: classes are objects too :-) What you want are instance variables that belong to the particular instance of the controller that gets created and runs the action. You can do this using a before_filter hook: class ApplicationController < ActionController::Base before_filter :set_user def set_user @current_user = User.find(session[:user_id]) end end etc. You''d probably want to limit it to certain actions, but you can do that too. (See API docs for before_filter and other related methods.) Notice that @current_user is inside an instance method definition. So when that code gets executed, it will be in the context of a particular instance of ApplicationController, and that instance will own the instance variable. David -- David A. Black (dblack@wobblini.net) * Ruby Power and Light, LLC (http://www.rubypowerandlight.com) > Ruby and Rails consultancy and training * Author of "Ruby for Rails" from Manning Publications! > http://www.manning.com/black
Jeff Ward
2006-Jun-02 01:41 UTC
[Rails] Set instance variable for all actions in the Controller
Wow, an "a ha" moment for me. This helps out tremendously. It shows in your response that you are an educator. I have your book and it also has helped me fudge my way through learning Ruby. Thank-you for writing one of the first Rails books when we are so short on good, professional documentation. On 6/1/06, dblack@wobblini.net <dblack@wobblini.net> wrote:> > Hi -- > > On Thu, 1 Jun 2006, Jeff Ward wrote: > > > Can I set an application wide instance variable that is available for > all > > actions...and their views? > > > > eg. > > class ApplicationController < ActionController::Base > > @current_user = User.find(session[:user_id]) > > end > > > > and everywhere I can call @current_user.id and I can get that object? > Even > > down in the views? > > > > I could not get this to work... > > What you''ve got there is an instance variable, but it''s an instance > variable that belongs to the class object, ApplicationController, > itself. Like so many things in Ruby, it comes down to: classes are > objects too :-) > > What you want are instance variables that belong to the particular > instance of the controller that gets created and runs the action. You > can do this using a before_filter hook: > > class ApplicationController < ActionController::Base > before_filter :set_user > def set_user > @current_user = User.find(session[:user_id]) > end > end > > etc. You''d probably want to limit it to certain actions, but you can > do that too. (See API docs for before_filter and other related > methods.) > > Notice that @current_user is inside an instance method definition. So > when that code gets executed, it will be in the context of a > particular instance of ApplicationController, and that instance will > own the instance variable. > > > David > > -- > David A. Black (dblack@wobblini.net) > * Ruby Power and Light, LLC (http://www.rubypowerandlight.com) > > Ruby and Rails consultancy and training > * Author of "Ruby for Rails" from Manning Publications! > > http://www.manning.com/black > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060602/dd1ac117/attachment.html
Michael Daines
2006-Jun-02 03:57 UTC
[Rails] Set instance variable for all actions in the Controller
> class ApplicationController < ActionController::Base > before_filter :set_user > def set_user > @current_user = User.find(session[:user_id]) > end > endYou could also do this: class ApplicationController < ActionController::Base protected def current_user @current_user ||= User.find(session[:user_id]) end helper_method :current_user end Which means that you don''t look for the current user until you actually need to, that the implementation of getting the current user is hidden a little better, and lets you type one less character to type when getting the current user. -- Michael Daines http://www.mdaines.com
Jeff Ward
2006-Jun-02 07:12 UTC
[Rails] Set instance variable for all actions in the Controller
Great, this works too and I''ll use it. I like that it doesn''t get set until I call for the variable. The only thing is I can''t restrict it to certain actions but for my purposes this doesn''t matter because I''m only calling it from actions where I need it so that''s good enough. Thanks, Jeff On 6/1/06, Michael Daines <michael.daines@gmail.com> wrote:> > > class ApplicationController < ActionController::Base > > before_filter :set_user > > def set_user > > @current_user = User.find(session[:user_id]) > > end > > end > > You could also do this: > > class ApplicationController < ActionController::Base > > protected > > def current_user > @current_user ||= User.find(session[:user_id]) > end > helper_method :current_user > > end > > Which means that you don''t look for the current user until you > actually need to, that the implementation of getting the current user > is hidden a little better, and lets you type one less character to > type when getting the current user. > > > -- > Michael Daines > http://www.mdaines.com > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060602/b07e9101/attachment.html
Vincent AE Scott
2006-Jul-28 11:08 UTC
[Rails] Set instance variable for all actions in the Controller
#if animikii /* Jun 02, 12:11 */> > Great, this works too and I''ll use it. I like that it doesn''t get set > until I call for the variable. The only thing is I can''t restrict it > to certain actions but for my purposes this doesn''t matter because I''m > only calling it from actions where I need it so that''s good enough. > Thanks, > Jeffif you want to restrict access to certain actions then i suggest you take a look at ActiveRBAC [0] which will allow you to quite easily specify such restrictions, like this: class MyController < ApplicationController before_filter :protect_controller, :except => [ :list, :index, :show ] def protect_controller_to_role( role ) if !session[:rbac_user_id].nil? and User.find(session[:rbac_user_id]).has_role?( "Admin", role ) return true else redirect_to ''/q/list'' flash[:notice] = "You are not allowed to access this page" return false end end def protect_controller protect_controller_to_role("BasicUser") end end the above restricts access to all actions in the controller except for list, index and show, unless they have been granted the Admin or BasicUser role. have a look at the manual [1] for more information on what can be done. -v [0] https://activerbac.turingstudio.com/trac [1] https://activerbac.turingstudio.com/releases/ActiveRbacManual.pdf> > On 6/1/06, Michael Daines <[1]michael.daines@gmail.com> wrote: > > > class ApplicationController < ActionController::Base > > before_filter :set_user > > def set_user > > @current_user = User.find(session[:user_id]) > > end > > end > You could also do this: > class ApplicationController < ActionController::Base > protected > def current_user > @current_user ||= User.find(session[:user_id]) > end > helper_method :current_user > end > Which means that you don''t look for the current user until you > actually need to, that the implementation of getting the current > user > is hidden a little better, and lets you type one less character to > type when getting the current user. > -- > Michael Daines > [2]http://www.mdaines.com > _______________________________________________ > Rails mailing list > [3]Rails@lists.rubyonrails.org > [4]http://lists.rubyonrails.org/mailman/listinfo/rails > > References > > 1. mailto:michael.daines@gmail.com > 2. http://www.mdaines.com/ > 3. mailto:Rails@lists.rubyonrails.org > 4. http://lists.rubyonrails.org/mailman/listinfo/rails> _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails#endif /* animikii@gmail.com */ -- keys: http://codex.net/gpg.asc I did''nt notice when the world went to shit. I was watching TV.
Robert Mela
2006-Aug-16 03:04 UTC
[Rails] Set instance variable for all actions in the Controller
before_filter :set_myvar protected def set_myvar @myvar=''abcdefg'' end This probably isn''t kosher -- controller variables should be set from inside controllers, not layouts... but it''s neat enough that I wish there were a good reason to use it... you could include this in your layout: <% @controller.instance_eval { @myvar=''foo'' } %> Jeff Ward wrote:> Can I set an application wide instance variable that is available for > all actions...and their views? > > eg. > class ApplicationController < ActionController::Base > @current_user = User.find(session[:user_id]) > end > > and everywhere I can call @current_user.id and I can get that object? > Even down in the views? > > I could not get this to work... > > Thanks in advance, > > Jeff > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- A non-text attachment was scrubbed... Name: rob.vcf Type: text/x-vcard Size: 116 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060816/19ec9238/rob.vcf