In xml responses, if the result is an empty array we want the response to look like this: <posts type="array"/> not like this (the default): <nil-classes type="array"/> This refactors controllers so that this is done automatically instead of having to manually call `@things.to_xml(root: "things")` everywhere. We do this by overriding the behavior of `respond_with` in `ApplicationResponder` to set the `root` option by default in xml responses.
13 lines
422 B
Ruby
13 lines
422 B
Ruby
# https://github.com/plataformatec/responders
|
|
# https://github.com/plataformatec/responders/blob/master/lib/action_controller/responder.rb
|
|
class ApplicationResponder < ActionController::Responder
|
|
# this is called by respond_with for non-html, non-js responses.
|
|
def to_format
|
|
if format == :xml
|
|
options[:root] ||= resource.table_name.dasherize if resource.respond_to?(:table_name)
|
|
end
|
|
|
|
super
|
|
end
|
|
end
|