Forum: Ruby on Rails Does render() have 2 be last statement in controller method?

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.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-04-17 22:39
I have a controller method that is attempting to do a render based on a
potential error condition.

So I have

if (condition)
  render()
else
  ..other code
end

....other code in method

Should I expect the render to take place immediately or does the render
call not work because it isn't the last statement in the method?

Thanks,
Wes
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-04-17 22:53
Wes Gamble wrote:
> I have a controller method that is attempting to do a render based on a
> potential error condition.
>
> So I have
>
> if (condition)
>   render()
> else
>   ..other code
> end
>
> ....other code in method
>
> Should I expect the render to take place immediately or does the render
> call not work because it isn't the last statement in the method?
>
> Thanks,
> Wes

render doesn't have to be the last function called. However, make sure
you call "return" after you do call render if you don't have
conditionals for both true and false branches. You'll get an error if
you have two render calls in the same method. Render does not
automatically return by itself.
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-17 22:59
(Received via mailing list)
Wes,

render() doesn't have to be the last thing you call in a controller
action, you just need to make sure that render() should only be
called once per action.

But render() doesn't magically stop processing in your action.  So if
you're trying to avoid reaching ".... other code in method" then you
need to 'return' in your controller method:

if (condition)
   render()
   return
else
....

A more succinct way is to say:

if (condition)
   return render()
else
...

Some people don't like having multiple exit points in a method.  As a
guideline I agree, it's not a great habit to get into but it does
have its place, and imho this is one of them.

Regards,
Trevor

--
Trevor Squires
http://somethinglearned.com
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-04-17 23:03
Thanks Trevor and Bryan,

That's what I needed.

Wes

Trevor Squires wrote:
> Wes,
>
> render() doesn't have to be the last thing you call in a controller
> action, you just need to make sure that render() should only be
> called once per action.
>
> But render() doesn't magically stop processing in your action.  So if
> you're trying to avoid reaching ".... other code in method" then you
> need to 'return' in your controller method:
>
> if (condition)
>    render()
>    return
> else
> ....
>
> A more succinct way is to say:
>
> if (condition)
>    return render()
> else
> ...
>
> Some people don't like having multiple exit points in a method.  As a
> guideline I agree, it's not a great habit to get into but it does
> have its place, and imho this is one of them.
>
> Regards,
> Trevor
>
> --
> Trevor Squires
> http://somethinglearned.com
This topic is locked and can not be replied to.