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.
Wes G. (Guest)
on 2006-05-26 01: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
Wes G. (Guest)
on 2006-05-26 02:08
Wes G. 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
Ezra Z. (Guest)
on 2006-05-26 02:27
(Received via mailing list)
On May 25, 2006, at 2:51 PM, Wes G. wrote:

>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> 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
Wes G. (Guest)
on 2006-05-26 02:34
Wes G. wrote:
> Wes G. 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.
Wes G. (Guest)
on 2006-05-26 02:36
Ezra Z. wrote:
> On May 25, 2006, at 2:51 PM, Wes G. wrote:
>
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> Rails mailing list
>> removed_email_address@domain.invalid
>> 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
Wes G. (Guest)
on 2006-05-26 02: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.