From 20bb76848b852bba9ab3c99b1c2a68464585bd56 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 17 Aug 2016 16:11:46 +0200 Subject: bugfix: send 406 if an unexpected format is asked for It used to run the action and then trigger a 500 because the template was not found. fixes !3 . --- app/controllers/application_controller.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2af2f29..61ced21 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,6 @@ class ApplicationController < ActionController::Base protect_from_forgery + before_action :check_mime_types before_filter :set_locale before_filter :no_cache_header before_filter :no_frame_header @@ -9,8 +10,26 @@ class ApplicationController < ActionController::Base ActiveSupport.run_load_hooks(:application_controller, self) + # by default we only respond to html. + # If you want to respond with json you are probably working on + # an ApiController. + respond_to :html + protected + # UPGRADE: this won't be needed in Rails 5 anymore as it's the default + # behavior if a template is present but a different format would be + # rendered and that template is not present + def check_mime_types + mimes = collect_mimes_from_class_level() + return if mimes.empty? + + collector = ActionController::MimeResponds::Collector.new(mimes, request.variant) + unless collector.negotiate_format(request) + raise ActionController::UnknownFormat + end + end + def default_error_handler(exc) respond_to do |format| format.json { render_json_error(exc) } -- cgit v1.2.3