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.
Wes G. (Guest)
on 2006-04-18 00: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
Bryan D. (Guest)
on 2006-04-18 00:53
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.
Trevor S. (Guest)
on 2006-04-18 00: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 S.
http://somethinglearned.com
Wes G. (Guest)
on 2006-04-18 01:03
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
This topic is locked and can not be replied to.