Matthew Margolis
2006-Jul-02 14:39 UTC
[Rails] 2 before_filters, only want one to render something
I have two before_filters for a few of my controllers. They are running my own methods authorize and admin_authorize. authorize is called on just about every action to make sure that a user is logged in. admin_authorize is called on about 80% of the actions and is used to make sure that a user is an administrator. If a user tries to access an admin_authorize protected action without being logged in then both authorize and admin_authorize trigger and both have redirects in them. This if course makes rails complain about two renders when only one is allowed. Is there a way that I can make authorize skip admin_authorize? Or will I have to go through and pick only one method to be before_filtered for each action? def authorize #inspired by rails recipes unless session[:user] flash[:notice] = "Please log in" session[:intended_action] = action_name session[:intended_controller] = controller_name session[:intended_id] = params[:id] || nil redirect_to(:controller => "account", :action => "login") return end end def admin_authorize unless session[:user] && User.find(session[:user]).admin? redirect_to :controller=> "groups", :action => "index" end end Thank you, Matthew Margolis blog.mattmargolis.net
Julian ''Julik'' Tarkhanov
2006-Jul-02 15:06 UTC
[Rails] 2 before_filters, only want one to render something
On 2-jul-2006, at 16:40, Matthew Margolis wrote:> I have two before_filters for a few of my controllers. They are > running my own methods authorize and admin_authorize. authorize is > called on just about every action to make sure that a user is > logged in. admin_authorize is called on about 80% of the actions > and is used to make sure that a user is an administrator. If a > user tries to access an admin_authorize protected action without > being logged in then both authorize and admin_authorize trigger and > both have redirects in them. This if course makes rails complain > about two renders when only one is allowed. Is there a way that I > can make authorize skip admin_authorize? Or will I have to go > through and pick only one method to be before_filtered for each > action?Return false from a filter method to halt the filter chain execution. -- Julian ''Julik'' Tarkhanov please send all personal mail to me at julik.nl
Chris Carter
2006-Jul-02 15:18 UTC
[Rails] Re: 2 before_filters, only want one to render something
before_filter :authorize, :only => [:action1, :action2] before_filter :auth_admin, :except => [:action1, :action2] Matthew Margolis wrote:> I have two before_filters for a few of my controllers. They are running > my own methods authorize and admin_authorize. authorize is called on > just about every action to make sure that a user is logged in. > admin_authorize is called on about 80% of the actions and is used to > make sure that a user is an administrator. If a user tries to access an > admin_authorize protected action without being logged in then both > authorize and admin_authorize trigger and both have redirects in them. > This if course makes rails complain about two renders when only one is > allowed. Is there a way that I can make authorize skip > admin_authorize? Or will I have to go through and pick only one method > to be before_filtered for each action?-- Posted via ruby-forum.com.
Matthew Margolis
2006-Jul-02 16:05 UTC
[Rails] 2 before_filters, only want one to render something
Julian ''Julik'' Tarkhanov wrote:> > On 2-jul-2006, at 16:40, Matthew Margolis wrote: > >> I have two before_filters for a few of my controllers. They are >> running my own methods authorize and admin_authorize. authorize is >> called on just about every action to make sure that a user is logged >> in. admin_authorize is called on about 80% of the actions and is >> used to make sure that a user is an administrator. If a user tries >> to access an admin_authorize protected action without being logged in >> then both authorize and admin_authorize trigger and both have >> redirects in them. This if course makes rails complain about two >> renders when only one is allowed. Is there a way that I can make >> authorize skip admin_authorize? Or will I have to go through and >> pick only one method to be before_filtered for each action? > > Return false from a filter method to halt the filter chain execution. > > --Julian ''Julik'' Tarkhanov > please send all personal mail to > me at julik.nl > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > lists.rubyonrails.org/mailman/listinfo/railsPerfect. Thank you. Matthew Margolis blog.mattmargolis.net