From c7ea86315123bd75a62d3911c961e0dbb0591861 Mon Sep 17 00:00:00 2001 From: Jacob Carlborg Date: Thu, 19 Dec 2024 15:57:25 +0100 Subject: [PATCH] Change default response format of the show action to HTML This is consistent with the other actions. When making HTTP requests using tools that are not web browsers, like `curl` or `fetch` in JavaScript without an explicit `Accept` header Rails will receive the request as with the `Accept` header set to `*/*`. That means that all registered mime types are a potential match and Rails will pick the response type based on the order they're declared in the source code. --- lib/rails_admin/config/actions/show.rb | 2 +- spec/integration/actions/show_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/rails_admin/config/actions/show.rb b/lib/rails_admin/config/actions/show.rb index 18d587399e..6187a371aa 100644 --- a/lib/rails_admin/config/actions/show.rb +++ b/lib/rails_admin/config/actions/show.rb @@ -21,8 +21,8 @@ class Show < RailsAdmin::Config::Actions::Base register_instance_option :controller do proc do respond_to do |format| - format.json { render json: @object } format.html { render @action.template_name } + format.json { render json: @object } end end end diff --git a/spec/integration/actions/show_spec.rb b/spec/integration/actions/show_spec.rb index c08b3619ae..881c8d49e2 100644 --- a/spec/integration/actions/show_spec.rb +++ b/spec/integration/actions/show_spec.rb @@ -47,6 +47,16 @@ end end + context 'with default format' do + it 'responds with HTML' do + page.driver.options[:headers] = {'HTTP_ACCEPT' => '*/*'} + visit show_path(model_name: 'team', id: team.id) + + response_type = Mime::Type.parse(response_headers['Content-Type']).first + expect(response_type).to be_html + end + end + context 'when compact_show_view is enabled' do it 'hides nil fields in show view by default' do visit show_path(model_name: 'team', id: team.id)