Created
July 18, 2014 11:47
-
-
Save rosenfeld/dddd0f285fa024fe76fc to your computer and use it in GitHub Desktop.
Supporting automatic return from render/redirect/head in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Please don't comment in this gist since I'm not notified by any comments here: | |
# https://github.com/isaacs/github/issues/21 | |
# This is the discussion to comment on: http://blog.arkency.com/2014/07/4-ways-to-early-return-from-a-rails-controller/ | |
class ApplicationController < ActionController::Base | |
# ... | |
around_action :catch_halt | |
def render(*args) | |
super | |
throw :halt | |
end | |
def redirect(*args) | |
super | |
throw :halt | |
end | |
def head(*args) | |
super | |
throw :halt | |
end | |
private | |
def catch_halt | |
catch :halt do | |
yield | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleController < ApplicationController | |
def my_action | |
render body: 'response' | |
render body: "double rendering won't happen as this will never be executed" | |
end | |
end |
I agree it's more elegant and I'd prefer it in the core too, but I'm afraid this could break third-party code that expect render/redirect/head not to return...
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using it! Man, I wish this was core, it's so much more elegant than other solutions.