Setting a global before_filter action in application.rb


#1

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


#2

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


#3

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


#4

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.


#5

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


#6

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