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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-01-10 21: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
renders
if you do

render :action => 'new' and return false

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

Thanks,

bakki kudva
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-01-10 21: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
it*.

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
  end
  #otherwise it will just render the first_action view
end

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

Hope this helps some.
-Nick
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-01-10 21:47
(Received via mailing list)
Nick,

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

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

I get the error because of the render statement within the unless ---
end
block. I had assumed that a render statement terminates the action. But
it
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
sort
of imply that you are done with the action, just like the redirect_to
and
any statements following it wouldn't be reached?

bakki
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-10 21: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 ...
end

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 ;-)
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 Kyle Maxwell (Guest)
on 2006-01-10 22:32
(Received via mailing list)
On 1/10/06, Bakki Kudva <bakki.kudva@gmail.com> 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
> >
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>

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

--
Kyle Maxwell
Chief Technologist
E Factor Media // FN Interactive
kyle@efactormedia.com
1-866-263-3261
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-01-10 22:38
(Received via mailing list)
Good point Jules, Thanks for pointing that out. I am still cutting my
teeth
on this :)

-bakki
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-01-10 22:44
(Received via mailing list)
That's what I was thinking...so why does rails give me the error
message? It
should not see the redirect_to below right?

-bakki
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-01-11 02: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.