Skip to content

Instantly share code, notes, and snippets.

@Hampei
Last active August 29, 2015 14:06
Show Gist options
  • Save Hampei/4ebe71928e05ead5bb78 to your computer and use it in GitHub Desktop.
Save Hampei/4ebe71928e05ead5bb78 to your computer and use it in GitHub Desktop.
xhr aware responder
# Will render the the show method instead of redirecting when the action is an ajax request.
# Also adds an options xhr_show to render instead of show.
class XhrAwareResponder < ActionController::Responder
protected
def navigation_behavior(error)
if get?
raise error
elsif has_errors? && default_action
render action: default_action, layout: options[:layout]
elsif controller.request.xhr?
render action: options[:xhr_show] || :show, layout: options[:layout]
else
redirect_to navigation_location
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment