public
Last active

  • Download Gist
app_responder.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
# Customizing your Responder to always redirect to the collection path (index action).
# I did not test this (yet), but it probably is really close.
class AppResponder < ActionController::Responder
protected
 
# Overwrite navigation_behavior to redirect to the collection_location.
def navigation_behavior(error)
if get?
raise error
elsif has_errors? && default_action
render :action => default_action
else
redirect_to collection_location
end
end
 
# Returns the collection location for redirecting after POST/PUT/DELETE.
# This method, converts the following resources array to the following:
#
# [:admin, @post] #=> [:admin, :posts]
# [@user, @post] #=> [@user, :posts]
#
# This new arrays, when given to redirect_to, will generate the proper URL
# pointing to the index action.
#
# [:admin, @post] #=> admin_posts_url
# [@user, @post] #=> user_posts_url(@user.to_param)
#
def collection_location
return options[:location] if options[:location]
klass = resources.last.class
 
if klass.respond_to?(:model_name)
resources[0...-1] << klass.model_name.plural.to_sym
else
resources
end
end
end
 
# And now, just add it to your controller:
class ApplicationController < ActionController::Base
self.responder = AppResponder
end

Thank you for the presentation yesterday. Didn't know about ActionController::Responder before.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.