Hello List, i am newby to rails and i am stuck with a problem concerning rails session and controllers. I tried to write a class which should be used as a before_filter. ------------------------- class AuthorizeFilter < ActionController::Base def self.filter(controller) user_id = session[:user_id] @perm = User.find(user_id).perm_strings unless @perm.include?(controller.action_name) flash[:notice] = "You do not have the permissions to access this functionality" jumpto = session[:jumpto] || { :action => "index" } session[:jumpto] = nil redirect_to(jumpto) and return false end end end ----------------------------- After invoking it as before_filter AuthorizeFilter I get the following error message: undefined local variable or method `flash'' for AuthorizeFilter:Class /app/controllers/authorize_filter.rb:14:in `filter'' script/server:49 Show framework trace c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/filters.rb:321:in `call_filters'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/filters.rb:313:in `each'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/filters.rb:313:in `call_filters'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/filters.rb:302:in `before_action'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/filters.rb:294:in `perform_action_without_benchmark'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/benchmarking.rb:41:in `perform_action_without_rescue'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/benchmarking.rb:41:in `measure'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/benchmarking.rb:41:in `perform_action_without_rescue'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/rescue.rb:80:in `perform_action'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/base.rb:356:in `send'' c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.9.1/lib/action_controller/base.rb:356:in `process'' c:/programme/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/dispatcher.rb:32:in `dispatch'' c:/programme/ruby/lib/ruby/gems/1.8/gems/rails- 0.13.1/lib/webrick_server.rb:105:in `handle_dispatch'' c:/programme/ruby/lib/ruby/gems/1.8/gems/rails- 0.13.1/lib/webrick_server.rb:71:in `service'' c:/programme/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'' c:/programme/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'' c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread'' c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start'' c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread'' c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start'' c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'' c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'' c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' c:/programme/ruby/lib/ruby/gems/1.8/gems/rails- 0.13.1/lib/webrick_server.rb:57:in `dispatch'' -------------------------------------------------------------- I tried other szenarios with diffrent controllers using the same session variable and everything worked. What is wrong? Govinda
Jamis Buck
2005-Aug-01 13:26 UTC
Re: Session and Controller: undefined local variable or method
On Aug 1, 2005, at 5:18 AM, Govinda wrote:> Hello List, > > i am newby to rails and i am stuck with a problem concerning rails > session and > controllers. > > I tried to write a class which should be used as a before_filter. > > > ------------------------- > class AuthorizeFilter < ActionController::Base > > def self.filter(controller) > user_id = session[:user_id] > @perm = User.find(user_id).perm_strings > unless @perm.include?(controller.action_name) > flash[:notice] = "You do not have the permissions to access > this functionality" > jumpto = session[:jumpto] || { :action => "index" } > session[:jumpto] = nil > redirect_to(jumpto) and return false > end > end > > endThe problem is that ''filter'' is being defined at the class level, but ''flash'' (and ''session'', and ''redirect_to'') are all defined at the instance level. What you probably want, instead, is: controller.session[...] controller.flash[...] controller.redirect_to(...) Although some (or all) of those may be protected methods, so you''d actually have to do: controller.send(:session)[...] controller.send(:flash)[...] controller.send(:redirect_to, ...) (And in case you''re wondering, you didn''t get an error on the #session call because there just happens to be a "session" method at the class level, but it is used for controlling the session configuration, not for accessing the session.) - Jamis> > ----------------------------- > > After invoking it as > > before_filter AuthorizeFilter > > I get the following error message: > > undefined local variable or method `flash'' for AuthorizeFilter:Class > /app/controllers/authorize_filter.rb:14:in `filter'' > script/server:49 > Show framework trace > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/filters.rb:321:in `call_filters'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/filters.rb:313:in `each'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/filters.rb:313:in `call_filters'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/filters.rb:302:in `before_action'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/filters.rb:294:in > `perform_action_without_benchmark'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/benchmarking.rb:41:in > `perform_action_without_rescue'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/benchmarking.rb:41:in `measure'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/benchmarking.rb:41:in > `perform_action_without_rescue'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/rescue.rb:80:in `perform_action'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/base.rb:356:in `send'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/actionpack- > 1.9.1/lib/action_controller/base.rb:356:in `process'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/ > dispatcher.rb:32:in > `dispatch'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/rails- > 0.13.1/lib/webrick_server.rb:105:in `handle_dispatch'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/rails- > 0.13.1/lib/webrick_server.rb:71:in `service'' > c:/programme/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'' > c:/programme/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'' > c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread'' > c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start'' > c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread'' > c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start'' > c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'' > c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'' > c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' > c:/programme/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' > c:/programme/ruby/lib/ruby/gems/1.8/gems/rails- > 0.13.1/lib/webrick_server.rb:57:in `dispatch'' > > -------------------------------------------------------------- > > I tried other szenarios with diffrent controllers using the same > session > variable and everything worked. > > What is wrong? > > Govinda > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >