# Does render() have 2 be last statement in controller method?

**URL:** <https://www.ruby-forum.com/t/does-render-have-2-be-last-statement-in-controller-method/94007>\
**Category:** Rails\
**Created:** [April 17, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/does-render-have-2-be-last-statement-in-controller-method/94007 "2006-04-17T22:39:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/does-render-have-2-be-last-statement-in-controller-method/94007/1 "2006-04-17T22:39:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/does-render-have-2-be-last-statement-in-controller-method/94007/2 "2006-04-17T22:53:05Z")

</div>

Wes G. 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.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/does-render-have-2-be-last-statement-in-controller-method/94007/3 "2006-04-17T22:59:00Z")

</div>

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 S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/does-render-have-2-be-last-statement-in-controller-method/94007/4 "2006-04-17T23:03:39Z")

</div>

Thanks Trevor and Bryan,

That’s what I needed.

Wes

Trevor S. 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 S.  
> [http://somethinglearned.com](http://somethinglearned.com)
