DoubleRenderError... again. ;-)

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
  • 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. :slight_smile: Thanks, Josh

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. :slight_smile: Thanks, Josh


Sincerely,

William P.

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. :slight_smile: Thanks, Josh


Sincerely,

William P.

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. :slight_smile:

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.

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