Providing context for Rails’ controller action
There are many scenarios where you might want the same controller action / method in your Rails application to perform multiple functions. For example, a wizard with multiple steps or a single form with multiple-stage AJAX calls. What you want to do is provide a 'context' to the specific request and have the controller handle that in some different way.
Until now, one way to do this would be to use a parameter on the URL (e.g.: http://yourapp/post/show/1?stage=something_here) and then simply branch the logic based on the parameter. Bruce Williams saw an opportunity to make things a little better, and has developed in_context. It still uses a parameter on the URL or in the request, but he made the branching mechanism somewhat cleaner.
The in_context page provides the best info, but here's a quick demonstration of the branching:
render :update do |page| # Stuff at this point happens regardless of context in_context do |context| context.nil? do # This happens if no context is passed end context.workflow_one do # One workflow goes through here.. end context.workflow_two do # And another through here.. end end end