# DoubleRenderError... again. ;-)

**URL:** <https://www.ruby-forum.com/t/doublerendererror-again/119451>\
**Category:** Rails\
**Created:** [November 7, 2007, 2:18am UTC](https://www.ruby-forum.com/t/doublerendererror-again/119451 "2007-11-07T02:18:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 7, 2007, 2:18am UTC](https://www.ruby-forum.com/t/doublerendererror-again/119451/1 "2007-11-07T02:18:03Z")

</div>

Hi all

I’m still having my troubles with DoubleRenderError in Rails. Some time  
ago I learnt that I should always return after redirecting to avoid this  
problem:

return redirect\_to :action =\> ‘list’

First question: do I have to return when using the render(…) method,  
too?

Now I’m still having a problem because I’m getting a DoubleRenderError.  
The trace looks like the following:

- I open the link [http://my-app.xx/member](http://my-app.xx/member)
- The default action index is launched in member\_controller:  
def index  
show  
render(:action =\> ‘show’) and return  
end
- The show action looks like this:  
if false # Just for testing purposes, should do some user  
authorization
# Everything’s OK, render the show.rhtml template like intended
else
# Display an error message and pretend that the site does not exist
return render(:file =\> “#{RAILS\_ROOT}/public/404.html”, :layout =\>  
false, :status =\> 404)  
end

I guess it’s clear why I’m getting the error message (after the rendered  
404.html file I get back into the index action and try to render the  
action ‘show’, too), but I don’t know how to avoid it in a clean way?

Or should I just redirect to action ‘show’ instead of calling it  
manually?

Any help is greatly appreciated. 🙂 Thanks, Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 7, 2007, 3:31am UTC](https://www.ruby-forum.com/t/doublerendererror-again/119451/2 "2007-11-07T03:31:55Z")

</div>

The simple way to put it is this. You can only call render or  
redirect\_to once per action, period. Neither one of these methods stop  
execution of an action either. So, you don’t have to use return with  
render or with redirect\_to as long another call to either of them is  
encountered in the path of execution. If you are running into the  
problem this frequently, it probably means you need to rethink how you  
are designing your actions.

-Bill

Joshua M. wrote:

> if false # Just for testing purposes, should do some user  
> action ‘show’, too), but I don’t know how to avoid it in a clean way?
> 
> Or should I just redirect to action ‘show’ instead of calling it  
> manually?
> 
> Any help is greatly appreciated. 🙂 Thanks, Josh

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 7, 2007, 3:34am UTC](https://www.ruby-forum.com/t/doublerendererror-again/119451/3 "2007-11-07T03:34:16Z")

</div>

The first thing I notice is that you are rendering show twice:

def index  
show  
render(:action =\> ‘show’) and return  
end

should just be

def index  
show  
end

Joshua M. wrote:

> if false # Just for testing purposes, should do some user  
> action ‘show’, too), but I don’t know how to avoid it in a clean way?
> 
> Or should I just redirect to action ‘show’ instead of calling it  
> manually?
> 
> Any help is greatly appreciated. 🙂 Thanks, Josh

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 7, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/doublerendererror-again/119451/4 "2007-11-07T12:23:15Z")

</div>

Thank you both.

So I guess I’ll have to rename my show.rhtml to index.rhtml… Would  
have been nice to not having to, but that’s OK. 🙂

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 7, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/doublerendererror-again/119451/5 "2007-11-07T16:11:29Z")

</div>

Thats true, but he does have a render in there, and thats why I said  
that. On second thought, a redirect\_to might be better and always render  
from show so that show can be called on it’s own as well.

-Bill

Frederick C. wrote:

> > should just be  
> > This should do the job  
> > end
> > 
> > > this  
> > > The trace looks like the following:
> > > 
> > > # Everything’s OK, render the show.rhtml template like intended
> > > 
> > > action ‘show’, too), but I don’t know how to avoid it in a clean way?  
> > > William P.
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 7, 2007, 1:53pm UTC](https://www.ruby-forum.com/t/doublerendererror-again/119451/6 "2007-11-07T13:53:13Z")

</div>

On 7 Nov 2007, at 02:33, William P. wrote:

> def index  
> show  
> end

That’s not quite true. Just calling show doesn’t make you render the  
show template (unless the show method itself calls render :action =\>  
‘show’)

This should do the job

def show  
if some\_authorisation\_condition  
… (don’t call render or redirect  
return true  
else  
render(:file =\> “#{RAILS\_ROOT}/public/404.html”, :layout  
=\>false, :status =\> 404)  
return false  
end  
end

def index  
if show  
render :action =\> ‘show’  
end  
end

Fred
