# How to overcome the DoubleRender problem?

**URL:** <https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795>\
**Category:** rubyonrails-talk\
**Created:** [October 29, 2007, 1:00am UTC](https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795 "2007-10-29T01:00:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Pratt](https://avatars.discourse-cdn.com/v4/letter/w/9dc877/32.png) [@William\_Pratt](https://discuss.rubyonrails.org/u/William_Pratt)\
**Post date:** [October 29, 2007, 1:00am UTC](https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795/1 "2007-10-29T01:00:09Z")

</div>

This should not create a problem at all as I do it all the time. My guess is that you are assuming that render and redirect\_to immediately return from a method, but they don't. All redirect\_to does is add a 302 header to the response object that is sent back to the browser. Without seeing your code, all I can do is assume, but try changing the redirect\_to line to something like the following (notice the && return):

redirect\_to(:controller =\> 'foo', :action =\> 'bar') && return

-Bill

Joshua Muheim wrote:

---

<div class="post-metadata">

**Author:** ![M\_Graff](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@M\_Graff](https://discuss.rubyonrails.org/u/M_Graff)\
**Post date:** [October 29, 2007, 1:08am UTC](https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795/2 "2007-10-29T01:08:11Z")

</div>

While this is perfectly valid syntax, I would recommend:

&nbsp;&nbsp;redirect\_to(...) &nbsp;&nbsp;return

because it is more clear what is going on. The && trick works -- unless redirect\_to suddenly starts returning true.

--Michael

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Rick\_DeNatale](https://discuss.rubyonrails.org/u/Rick_DeNatale)\
**Post date:** [October 29, 2007, 1:13am UTC](https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795/3 "2007-10-29T01:13:38Z")

</div>

Actually the return will happen as long as the redirect\_to returns anything but nil or false.

---

<div class="post-metadata">

**Author:** ![M\_Graff](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@M\_Graff](https://discuss.rubyonrails.org/u/M_Graff)\
**Post date:** [October 29, 2007, 1:29am UTC](https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795/4 "2007-10-29T01:29:41Z")

</div>

Gah, brain fart. 🙂

The point is, && is "short-circuit" in that the first in the path that fails causes the rest to not even run.

--Michael

---

<div class="post-metadata">

**Author:** ![Herryanto\_Siatono](https://avatars.discourse-cdn.com/v4/letter/h/e8c25b/32.png) [@Herryanto\_Siatono](https://discuss.rubyonrails.org/u/Herryanto_Siatono)\
**Post date:** [October 29, 2007, 5:51am UTC](https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795/5 "2007-10-29T05:51:01Z")

</div>

You can also try out:

return redirect\_to(...)

You can avoid the "short-circuit".

- Herry

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [October 29, 2007, 3:28pm UTC](https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795/6 "2007-10-29T15:28:31Z")

</div>

> which calls the method member\_requests():
> 
> &nbsp;&nbsp;def member\_requests(right\_name, obj, options = {}, &block) &nbsp;&nbsp;&nbsp;&nbsp;if logged\_in\_member.has\_system\_right?(SystemRight.find\_by\_model\_class\_and\_name(obj.to\_s, right\_name)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield if block\_given? &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :file =\> "#{RAILS\_ROOT}/public/404.html", :layout =\> true, :status =\> 404 &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end
> 
> And that's where the DoubleRenderError is thrown! Anyone can see where the problem is? I just want to render the 404.html file, nothing else... No double rendering or stuff! 😛

As far as I can see you then trickle down to

> model\_obj.new\_record? ? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render(:action =\> :new) : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render(:action =\> :edit)

Which renders a second time, hence the error. I can also see quite a few other paths by which this could happen.

Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [October 29, 2007, 7:40pm UTC](https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795/7 "2007-10-29T19:40:28Z")

</div>

that there are other cases. eg member\_requests rendering and you later redirecting.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [October 29, 2007, 9:50pm UTC](https://discuss.rubyonrails.org/t/how-to-overcome-the-doublerender-problem/19795/8 "2007-10-29T21:50:32Z")

</div>

It's dead simple. If you've called render or redirect\_to once in an action, make sure you don't do either of them again.
