I am wondering why there is no double-render when there is a redirect_to
or render
in before_action
. Consider this example:
class SomeController < ApplicationController
before_action :callback
def new
callback2
render 'new'
end
def callback
render 'new'
end
def callback2
render 'new'
end
end
I see that before_action
will be useless if it can't redirect but how it is made? If I comment the before_action
it will throw exception.
How is before_action
implemented to not cause double-render?
When explicitly calling render from a controller, it checks that no other response_body was previously set (e.g. via render or redirect_to ). If there is already a response_body, it raises AbstractController::DoubleRenderError.
But, I have to warn you: Running code before render is usually a sign that you’re going against the grain of how React works. It makes perfect sense to think “I want to fetch data before my component renders”. Logical! But not how React works. React does not wait to render. Ever.
Initializing state actually does run before the first render, and leaving it uninitialized is a common source of problems. This leads to errors like Cannot read property 'map' of undefined' when the component tries to render before the data is ready.
In the context of a view, the render method can be called multiple times to render partials. No specific restrictions apply to this implementation, so no exceptions will be raised in this case. When explicitly calling render from a controller, it checks that no other response_body was previously set (e.g. via render or redirect_to ).
See the Rails Guide on controllers :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With