Hello everybody! The API-documentation for ActionController::Filters::ClassMethods describes, that you can use return and redirects to stop the processing chain from a before_filter. There''s the following example: before_filter { |controller| return false if controller.params["stop_action"] } This doesn''t work (ThreadError) here (Rails 0.12.1, Ruby 1.8.2, Linux), IMHO because all blocks and Proc.new-objects allow return only if the return would be allowed in the scope in which they are defined. This block isn''t defined in a function, so there''s an error. The following works fine: before_filter(lambda { |controller| return false if controller.params["stop_action"] }) Is this a failure in the documentation or is it me making a mistake? Also it is not possible for me to make a call to redirect_to in such a block (NoMethodError) nor is it possible to invoke it with the instance (controller.redirect_to -> call to a protected method). So I use controller.send(:redirect_to) which I consider a bit messy. Has anyone a better solution? Thanks, Michael
> before_filter { |controller| return false if controller.params > ["stop_action"] } > > This doesn''t work (ThreadError) here (Rails 0.12.1, Ruby 1.8.2, > Linux), IMHO because all blocks and Proc.new-objects allow return only > if the return would be allowed in the scope in which they are defined. > This block isn''t defined in a function, so there''s an error. The > following works fine:I''m not sure about this particular case, but most blocks that depend on a return value (that I am aware of) just evaluate the block itself (not expecting an explicit ''return''). For example: before_filter { |controller| false if controller.params["stop_action"] } Might that do the trick? Duane Johnson (canadaduane)
> I''m not sure about this particular case, but most blocks that depend > on a return value (that I am aware of) just evaluate the block itself > (not expecting an explicit ''return''). For example: > > before_filter { |controller| false if controller.params["stop_action"] } > > Might that do the trick?It does the trick! Thank you for the suggestion! I have issued a ticket for this on http://dev.rubyonrails.com/ticket/1291