Forum: Ruby on Rails Setting a global before_filter action in application.rb

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-05-25 23:51
Can you set a global before_filter action in application.rb?

So, for example, you could control authentication for all of the
controllers in an app.?

Obviously, you would need a way to reference actions by controller
within this "global before-filter".

Thanks,
Wes
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-05-26 00:08
Wes Gamble wrote:
> Can you set a global before_filter action in application.rb?
>
> So, for example, you could control authentication for all of the
> controllers in an app.?
>
> Obviously, you would need a way to reference actions by controller
> within this "global before-filter".
>
> Thanks,
> Wes

Got it!!!

Wow, that was really easy.

If anyone's interested in the general case of the Authenticate recipe,
so that you can apply it to all of your app's controllers, I have a way
to do it.

Wes
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-05-26 00:27
(Received via mailing list)
On May 25, 2006, at 2:51 PM, Wes Gamble wrote:

>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails


Wes-

	Yeah you can put global before_filter in application.rb. thats what
its there for. All your other controllers inherit from this one. Just
set it up like you would think. Then if you want to overide it in
individual controllers you use this syntax in a normal controller:

class ApplicationController < ActionController::Base
   before_filter :authenticate_user

   protected

   def authenticate_user
     # authentication code here
   end

end



class FooController < ActionController

    skip_before_filter :authenticate_user, :only =>
[:index, :list, :show]

   #rest of controller

end


-Ezra
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-05-26 00:34
Wes Gamble wrote:
> Wes Gamble wrote:
>> Can you set a global before_filter action in application.rb?
>>
>> So, for example, you could control authentication for all of the
>> controllers in an app.?
>>
>> Obviously, you would need a way to reference actions by controller
>> within this "global before-filter".
>>
>> Thanks,
>> Wes
>
> Got it!!!
>
> Wow, that was really easy.
>
> If anyone's interested in the general case of the Authenticate recipe,
> so that you can apply it to all of your app's controllers, I have a way
> to do it.
>
> Wes

I spoke too soon.  My error scenarios fail looking for the generic stuff
in the specific places.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-05-26 00:36
Ezra Zygmuntowicz wrote:
> On May 25, 2006, at 2:51 PM, Wes Gamble wrote:
>
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
> Wes-
>
> 	Yeah you can put global before_filter in application.rb. thats what
> its there for. All your other controllers inherit from this one. Just
> set it up like you would think. Then if you want to overide it in
> individual controllers you use this syntax in a normal controller:
>
> class ApplicationController < ActionController::Base
>    before_filter :authenticate_user
>
>    protected
>
>    def authenticate_user
>      # authentication code here
>    end
>
> end
>
>
>
> class FooController < ActionController
>
>     skip_before_filter :authenticate_user, :only =>
> [:index, :list, :show]
>
>    #rest of controller
>
> end
>
>
> -Ezra


What about redirecting to templates?  I put a common login page in
'common/login_form.rhtml' (common is my directory under views).

But when the actions fail, they're trying to find a login_form template
underneath their specific view folders.

Wes
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-05-26 00:44
>
>
> What about redirecting to templates?  I put a common login page in
> 'common/login_form.rhtml' (common is my directory under views).
>
> But when the actions fail, they're trying to find a login_form template
> underneath their specific view folders.
>
> Wes

OK now I really got it.  I was rendering when I should have been
redirecting.

Still - a lot of fun!

wg
This topic is locked and can not be replied to.