Quy Nguyen Vu
Quy Nguyen Vu

Reputation: 181

DoubleRenderError when use render in both called and caller method in Rails

I am a newbie with Ruby on Rails.

This code returns DoubleRenderError:

class PostapisController < ApplicationController
    def method1
       method2()
       render json: {:success: false}
    end 

    def method2
       render json: {:status => 'ok'} and return
    end 
end

Why does it return DoubleRenderError? How can I fix it if I still want both method1() and method2() to call render?

Upvotes: 0

Views: 32

Answers (1)

Max Williams
Max Williams

Reputation: 32945

"render" and "redirect_to" are both ways to generate the response to the request your server received. The server can only give one response to each request, so you can't render twice.

In your case, you should be able to appreciate that it doesn't make sense to send a {:success: false} response AND send a {:status => 'ok'} response: it's got to be one or the other, right? That's not a rails thing, it's just a web thing. One response per request.

I don't know what you're trying to achieve, so can't advise any further, other than by telling you to read some more basic stuff about Rails before starting to use it.

Upvotes: 1

Related Questions