Skip to content

Instantly share code, notes, and snippets.

@jordaaash
Last active December 23, 2015 05:39
Show Gist options
  • Save jordaaash/6588798 to your computer and use it in GitHub Desktop.
Save jordaaash/6588798 to your computer and use it in GitHub Desktop.
When providing an alternative action for rendering to respond_with, the action is ignored when the resourceful response is successful, because options[:action] is deleted for reasons I can't determine. Changing line 11 of responder.rb, as demonstrated in patched_responder.rb, fixes this so that the action is correctly passed to default_render. T…
class MessagesController < ActionController::Base
respond_to :html
def show
resource = Message.find(params[:id])
action = resource.sent? ? :edit : :show
respond_with(resource, :action => action)
end
def edit
end
end
module ActionController
class Responder
# ...
def initialize(controller, resources, options={})
@controller = controller
@request = @controller.request
@format = @controller.formats.first
@resource = resources.last
@resources = resources
@options = options
@action = options.delete(:action)
@default_response = options.delete(:default_response)
end
# ...
def to_html
default_render
rescue ActionView::MissingTemplate => e
navigation_behavior(e)
end
# ...
def navigation_behavior(error)
if get?
raise error
elsif has_errors? && default_action
render :action => default_action
else
redirect_to navigation_location
end
end
# ...
def default_render
if @default_response
@default_response.call(options)
else
controller.default_render(options)
end
end
# ...
def default_action
@action ||= DEFAULT_ACTIONS_FOR_VERBS[request.request_method_symbol]
end
end
end
module ActionController
class Responder
# ...
def initialize(controller, resources, options={})
@controller = controller
@request = @controller.request
@format = @controller.formats.first
@resource = resources.last
@resources = resources
@options = options
@action = options[:action]
@default_response = options.delete(:default_response)
end
# ...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment