Forum: Ruby on Rails Can only render or redirect once per action - why?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bakki K. (Guest)
on 2006-01-10 22:14
(Received via mailing list)
I ran into this error message a quite a few times since my app requires
branching to different pages from the same action... say using a switch
statement. Ofcourse I found that you can use  multiple redirects or
if you do

render :action => 'new' and return false

I hate to code something I don't understand fully. Any explanation of
will be greatly appreciated!


bakki kudva
Nick S. (Guest)
on 2006-01-10 22:32
(Received via mailing list)
How would one send output to the browser twice? (minus ajax usuage of
course) Thats really the simple reasoning behind it. The render tells
rails what to send back to the browser and hence, only has one shot at

If you need to render another view from your current action, here's
the common approach:

def first_action
  if some_condition
     render :action => :second_action #renders the second action_view
but doesn't actually call the second_action method
  #otherwise it will just render the first_action view

def second_action
    if something
        redirect_to :action => "first_action"
        render :layout => false #just an example of rendering with no

Hope this helps some.
Bakki K. (Guest)
on 2006-01-10 22:47
(Received via mailing list)

Thanks for the quick repy. I realize you wouldn't send output to the
twice. For example in the following code...

  def create
    @user =[:user])
    @confirm = params[:confirm]
    unless @user.password == @confirm
      flash[:notice] = 'Password mismatch. Please type them again.'
      render :action => 'new' and return false
      flash[:notice] = 'Student was successfully created.'
      redirect_to :action => 'list'
      render :action => 'new'

I get the error because of the render statement within the unless ---
block. I had assumed that a render statement terminates the action. But
apparently does not unless you explicitly tell it to with the return
statement. I wonder if there is any reason for this. Doesn't a render
of imply that you are done with the action, just like the redirect_to
any statements following it wouldn't be reached?

Jules J. (Guest)
on 2006-01-10 22:59
I think it is for the case where you want to redirect a user because
he/she is not authorized:

unless user.authorized?
 redirect_to ...

render ...

The render call would be executed and if the user blocked the redirect
he/she would be able to see the page.

And validations should be in your model anyway. There is
validates_confirmation_of for this password-confirmation ;-)
Kyle M. (Guest)
on 2006-01-10 23:32
(Received via mailing list)
On 1/10/06, Bakki K. <removed_email_address@domain.invalid> wrote:
>        render :action => 'new' and return false
> block. I had assumed that a render statement terminates the action. But it
> > rails what to send back to the browser and hence, only has one shot at
> >   #otherwise it will just render the first_action view
> >

Render doesn't return, for reverse-compatibility reasons.

Kyle M.
Chief Technologist
E Factor Media // FN Interactive
Bakki K. (Guest)
on 2006-01-10 23:38
(Received via mailing list)
Good point Jules, Thanks for pointing that out. I am still cutting my
on this :)

Bakki K. (Guest)
on 2006-01-10 23:44
(Received via mailing list)
That's what I was why does rails give me the error
message? It
should not see the redirect_to below right?

Nick S. (Guest)
on 2006-01-11 03:00
(Received via mailing list)
It could either be seeing the redirect OR the render. Either one will
give you the error message. The way you have it setup now is that if
the first render is seen in the unless you will ALWAYS see another
render (either redirect in the if, or the render with your else).
Throw a return statement after the unless render and you'll be good to
go, or restructure it so its only got one exit point.
This topic is locked and can not be replied to.