Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails render and return in before_action, DoubleRenderError

In a controller action with a before_action, using render something and return does not actually cause the controller to stop executing the rest of the action. In my testing, only when called in the controller action does and return work as I would expect.

# DoubleRenderError
class SomeController < ApplicationController
    before_filter :double_render, only: [:index]

    def index
        render file: "public/500.html", status: :internal_server_error
    end

    def double_render
        render file: "public/404.html", status: :not_found and return
    end
end

# Renders 404 only, no error
class SomeController < ApplicationController
    def index
        render file: "public/404.html", status: :not_found and return
        render file: "public/500.html", status: :internal_server_error
    end
end

What's going on here? Can before_actions stop the rest of a controller's execution?

like image 850
brodney Avatar asked Aug 20 '14 16:08

brodney


2 Answers

If your intention is to render the 404 page, you shouldn't render it manually in the before_filter. The right way of doing that is raising a routing error, like this:

raise ActionController::RoutingError.new('Not Found')

You could implement a method called "render_404", raise this exception in it and let the routing system do the rest.

EDIT: Actually, your code should work. I really don't know what is going on. I wrote the same thing here and it worked - the action method is never called if the before_filter renders something. What version of Rails are you using?

like image 59
Victor Avatar answered Oct 13 '22 22:10

Victor


This is a pretty simple case of flow control.

Yes, when you and return from within the method, it obviously returns and doesn't reach the second render.

Nothing about rendering inside a before_filter is meant to stop the actual invocation of the action itself, and calling and return from a before_filter doesn't affect the execution of a completely separate method invocation.

This is pure Ruby flow-control, no Rails magic involved.

like image 21
meagar Avatar answered Oct 13 '22 23:10

meagar