Wes Gamble
2006-Apr-17 20:39 UTC
[Rails] Does render() have 2 be last statement in controller method?
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 -- Posted via http://www.ruby-forum.com/.
Bryan Duxbury
2006-Apr-17 20:53 UTC
[Rails] Re: Does render() have 2 be last statement in controller met
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, > Wesrender 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. -- Posted via http://www.ruby-forum.com/.
Trevor Squires
2006-Apr-17 20:58 UTC
[Rails] Does render() have 2 be last statement in controller method?
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 On 17-Apr-06, at 1:39 PM, 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Wes Gamble
2006-Apr-17 21:03 UTC
[Rails] Re: Does render() have 2 be last statement in controller met
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-- Posted via http://www.ruby-forum.com/.