Skip to content

Instantly share code, notes, and snippets.

@unakatsuo
Created July 3, 2010 16:49
Show Gist options
  • Save unakatsuo/462692 to your computer and use it in GitHub Desktop.
Save unakatsuo/462692 to your computer and use it in GitHub Desktop.
Rails forward action
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
protected
#
def forward_action(action_name)
raise "Unknown action: #{action_name}" unless self.respond_to?(action_name)
self.action_name = request.path_parameters['action'] = action_name.to_s
__send__(action_name)
end
end
@unakatsuo
Copy link
Author

This snippet brings the similar feature to JavaServlet's forward() to Rails. It was not enough to just call the action method inside of the requested action. It had to set the destination action name to the current request object as well as ActionController#action_name. Otherwise url_for() keeps to address the requested action path.

@unakatsuo
Copy link
Author

may work with 2.3 only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment