Skip to content

Commit 482b5ac

Browse files
authored
Merge pull request #2776 from chamun/destroy_record_from_index_page_status_code_404
Return status code 200 when destroying a record from the index page fails
2 parents 81f612f + 61f1c85 commit 482b5ac

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

app/controllers/rails_admin/application_controller.rb

+2
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ def rails_admin_controller?
6363
rescue_from RailsAdmin::ObjectNotFound do
6464
flash[:error] = I18n.t('admin.flash.object_not_found', model: @model_name, id: params[:id])
6565
params[:action] = 'index'
66+
@status_code = :not_found
6667
index
6768
end
6869

6970
rescue_from RailsAdmin::ModelNotFound do
7071
flash[:error] = I18n.t('admin.flash.model_not_found', model: @model_name)
7172
params[:action] = 'dashboard'
73+
@status_code = :not_found
7274
dashboard
7375
end
7476
end

lib/rails_admin/config/actions/dashboard.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Dashboard < RailsAdmin::Config::Actions::Base
3030
@most_recent_created[t.model.name] = t.model.last.try(:created_at)
3131
end
3232
end
33-
render @action.template_name, status: (flash[:error].present? ? :not_found : 200)
33+
render @action.template_name, status: @status_code || :ok
3434
end
3535
end
3636

lib/rails_admin/config/actions/index.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Index < RailsAdmin::Config::Actions::Base
4141

4242
respond_to do |format|
4343
format.html do
44-
render @action.template_name, status: (flash[:error].present? ? :not_found : 200)
44+
render @action.template_name, status: @status_code || :ok
4545
end
4646

4747
format.json do

spec/integration/basic/destroy/rails_admin_basic_destroy_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
it 'shows error message' do
4040
is_expected.to have_content('Player failed to be deleted')
4141
end
42+
43+
it 'returns status code 200' do
44+
expect(page.status_code).to eq(200)
45+
end
4246
end
4347

4448
describe 'destroy' do
@@ -88,4 +92,20 @@
8892
expect(URI.parse(page.current_url).path).to eq(show_path(model_name: 'player', id: @player.id))
8993
end
9094
end
95+
96+
describe 'destroy from index page' do
97+
it 'returns status code 200' do
98+
if Rails.version >= '5.0'
99+
allow_any_instance_of(Player).to receive(:destroy_hook) { throw :abort }
100+
else
101+
allow_any_instance_of(Player).to receive(:destroy_hook).and_return false
102+
end
103+
@player = FactoryGirl.create :player
104+
visit index_path(model_name: 'player')
105+
click_link 'Delete'
106+
click_button "Yes, I'm sure"
107+
108+
expect(page.status_code).to eq(200)
109+
end
110+
end
91111
end

0 commit comments

Comments
 (0)