Created
June 26, 2010 11:28
-
-
Save josevalim/453984 to your computer and use it in GitHub Desktop.
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
# Customizing your Responder to always redirect to the collection path (index action). | |
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] | |
# | |
# When these new arrays are given to redirect_to, it 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 |
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
# Customizing your Responder to always redirect to the collection path (index action). | |
class AppResponder < ActionController::Responder | |
protected | |
# 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] | |
# | |
# When these new arrays are given to redirect_to, it will generate the | |
# proper URL pointing to the index action. | |
# | |
# [:admin, @post] #=> admin_posts_url | |
# [@user, @post] #=> user_posts_url(@user.to_param) | |
# | |
def navigation_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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My pleasure! :D