Override Responder#to_format to avoid first missing render on POST
request. Fallback to default Rails responder behavior if template is not found
This commit is contained in:
parent
0cf1efea14
commit
35d21b38e6
@ -14,20 +14,33 @@ module RablRails
|
||||
end
|
||||
end
|
||||
|
||||
def to_format
|
||||
if get? || response_overridden?
|
||||
default_render
|
||||
elsif has_errors?
|
||||
display_errors
|
||||
else
|
||||
api_behavior(nil)
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def api_behavior(error)
|
||||
template = @controller.respond_to?(:responder_default_template, true) ? controller.send(:responder_default_template)
|
||||
: RablRails.responder_default_template
|
||||
rabl_options = options.merge(template: template)
|
||||
if post?
|
||||
template = if @controller.respond_to?(:responder_default_template, true)
|
||||
controller.send(:responder_default_template)
|
||||
else
|
||||
RablRails.responder_default_template
|
||||
end
|
||||
options[:template] ||= "#{@controller.controller_name}/#{template}"
|
||||
|
||||
if get?
|
||||
controller.default_render rabl_options
|
||||
elsif post?
|
||||
controller.default_render rabl_options.merge!(status: :created, location: api_location)
|
||||
controller.default_render options.merge(status: :created, location: api_location)
|
||||
else
|
||||
head :no_content
|
||||
end
|
||||
rescue ActionView::MissingTemplate => e
|
||||
super(e)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user