Quick Tip: Clean render/redirect_to And return

posted on Saturday, September 26, 2009 at 5:51 AM PDT by Slippy Douglas | 0 comments

I’ve often longed for a way to render something or redirect_to somewhere and finish the action’s execution at that point. Often, I resort to something messy like this:

if cant_move
  render :nothing => true 
  return
end

While playing with Rails today, I realized that there’s no reason (in the world of Ruby) that this shouldn’t work instead:

return render :nothing => true if cant_move

And it does work! I don’t know what it returns (if anything), but it doesn’t matter, since Rails doesn’t do anything with it. It looks a little funny at first with the return ren... in there, and although many Rubyists aren’t a huge fans of using the return keyword, it’s necessary for an “early-out” situation like this.

Now I need to go refactor a bunch of my code…

Post a comment


(required, but not displayed)

(optional, and awesome)

(required)