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