Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no double-render when using before_action?

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?

like image 909
Mariy Avatar asked May 11 '15 13:05

Mariy


People also ask

What is doublerendererror in abstractcontroller?

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.

Should you run code before or after render in react?

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.

What happens if you don't initialize a component before rendering?

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.

How many times can the render method be called to render?

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 ).


1 Answers

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.

like image 154
Baldrick Avatar answered Oct 22 '22 06:10

Baldrick