Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

before_filter not cancelling action

I'm having trouble getting before filters to work in a Rails app I have recently upgraded from 1.9(?) to 2.3.11. To try and debug it, I have put a before_filter in a controller:

before_filter :false_filter 

and the following in application_controller.rb:

def false_filter   puts "false filter running"   false end 

I then call the method from either cucumber/webrat or a browser, and while the filter is getting called (I can see the puts outputting the message), the filter chain isn't getting terminated.

I'm wondering if there's some boilerplate code that hasn't been generated. Can anyone suggest where to look?

like image 239
Henry Collingridge Avatar asked Jun 15 '11 05:06

Henry Collingridge


People also ask

What happens to the onactionexecuted method when a filter is cancelled?

Any pending OnActionExecuted and OnActionExecuting filters will not be invoked and the invoker will not call the OnActionExecuted method for the cancelled filter or for pending filters. The OnActionExecuted filter for previously run filters will run.

What is the difference between action filters and exception filters?

You also can use exception filters to log errors. Action filters are one of the most commonly used filters to perform additional data processing, or manipulating the return values or cancelling the execution of action or modifying the view structure at run time.

How to cancel a filter execution in MVC?

See my download sample and MSDN article Filtering in ASP.NET MVC. You can cancel filter execution in the OnActionExecuting and OnResultExecuting methods by setting the Result property to a non-null value.

What are action filters in ASP NET MVC framework?

ASP.NET MVC framework supports the following action filters − Action Filters − Action filters are used to implement logic that gets executed before and after a controller action executes. We will look at Action Filters in detail in this chapter.


1 Answers

Nothing pays any attention to a before-filter's return value. If you want to stop processing, you have to render something from your filter or redirect to somewhere else, from the fine guide:

If a before filter renders or redirects, the action will not run. If there are additional filters scheduled to run after that filter they are also cancelled.

The same text appears in the 5.2.0 guide.

This behavior does make sense, if the filter chain doesn't complete (i.e. stops filtering part way through) then you'd end up calling controller methods with things not set up the way they were expecting them to be and that would just cause pain, suffering, and confusion and that wouldn't be at all friendly or fun.

like image 119
mu is too short Avatar answered Oct 11 '22 01:10

mu is too short