As the title suggests, Rails doesn''t seem to recognize that a session
is a special hash extended off of ActionController::Base. The error
tells me either session is unrecognized or session is an array that
doesn''t accept symbols as a parameter.
I use before_filter and session as following:
I have several user controllers extending off of each other, like
this: AdminController < ModeraterController, ModeraterController <
MemberController, MemberController < ApplicationController.
In each controller, I either have a before_filter block saying
something like:
before_filter do |controller|
unless session[:member_id]
redirect_to :controller=>''login''
end
if controller.action_name==''edit_profile''
unless params[:id]==session[:member_id]
flash[:notice]="You can''t edit someone else''s
profile"
#some redirect command
end
end
end
I also tried the class method (with the self.filter(controller)
method), but with no avail, so I was wondering:
1) are you allowed to have more than one method of before_filter in
one controller? For example, is this valid?:
TempController < ApplicationController
before_filter :filter1, :only=>''method1''
before_filter :filter2, :except=>''method1''
#bunch of methods
end
2) why isn''t session recognized in the before_filter block method?
3) I''m guessing the reason why the class method isn''t working
is
because I didn''t extend it off of ApplicationController (though it is
classes floating in a Controller). If this is so, can I do something
like?:
TempController < ApplicationController
before_filter :self
#bunch of methods
self.filter(controller)
#whatever
end
end
Thanks for the help!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
> I also tried the class method (with the self.filter(controller) > method), but with no avail, so I was wondering: > 1) are you allowed to have more than one method of before_filter in > one controller? For example, is this valid?: > TempController < ApplicationController > before_filter :filter1, :only=>''method1'' > before_filter :filter2, :except=>''method1'' > #bunch of methods > endYup.> 2) why isn''t session recognized in the before_filter block method?It''s using the controller''s class as the local binding since a controller instance isn''t available at the time of the block''s creation. That''s why the controller instance is passed. Therefore, all controller methods need to be called from the controller instance, not self.> 3) I''m guessing the reason why the class method isn''t working is > because I didn''t extend it off of ApplicationController (though it is > classes floating in a Controller). If this is so, can I do something > like?: > TempController < ApplicationController > before_filter :self > #bunch of methods > self.filter(controller) > #whatever > end > endYes, but you don''t need to pass the controller. I find this makes the filter more self-describing. before_filter :check_member_access protected def check_member_access ... end The nice thing is you also have access to the private/protected methods of the controller. -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.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 -~----------~----~----~----~------~----~------~--~---
Huh, I should have tested this out earlier: it would have saved me a lot of time:). Thanks for your help! On Jul 31, 9:47 am, "Rick Olson" <technowee...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I also tried the class method (with the self.filter(controller) > > method), but with no avail, so I was wondering: > > 1) are you allowed to have more than one method of before_filter in > > one controller? For example, is this valid?: > > TempController < ApplicationController > > before_filter :filter1, :only=>''method1'' > > before_filter :filter2, :except=>''method1'' > > #bunch of methods > > end > > Yup. > > > 2) why isn''t session recognized in the before_filter block method? > > It''s using the controller''s class as the local binding since a > controller instance isn''t available at the time of the block''s > creation. That''s why the controller instance is passed. Therefore, > all controller methods need to be called from the controller instance, > not self. > > > 3) I''m guessing the reason why the class method isn''t working is > > because I didn''t extend it off of ApplicationController (though it is > > classes floating in a Controller). If this is so, can I do something > > like?: > > TempController < ApplicationController > > before_filter :self > > #bunch of methods > > self.filter(controller) > > #whatever > > end > > end > > Yes, but you don''t need to pass the controller. I find this makes the > filter more self-describing. > > before_filter :check_member_access > > protected > def check_member_access > ... > end > > The nice thing is you also have access to the private/protected > methods of the controller. > > -- > Rick Olsonhttp://lighthouseapp.comhttp://weblog.techno-weenie.nethttp://mephistoblog.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 -~----------~----~----~----~------~----~------~--~---