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 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?
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?
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.