Skip to content

Commit

Permalink
Fixes a bug where POST requests fail twice
Browse files Browse the repository at this point in the history
closes #48
  • Loading branch information
yuki24 committed Jun 3, 2019
1 parent cd9c4b0 commit d68d677
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/rambulance/exceptions_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module Rambulance
class ExceptionsApp < ActionController::Base
layout :layout_name

if self.respond_to?(:skip_forgery_protection)
skip_forgery_protection
end

def self.call(env)
exception = env["action_dispatch.exception"]
status_in_words = if exception
Expand Down
11 changes: 7 additions & 4 deletions lib/rambulance/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
module Rambulance
class Railtie < Rails::Railtie
initializer 'rambulance', after: :prepend_helpers_path do |app|
require "rambulance/exceptions_app"
ActiveSupport.on_load(:action_controller) do
require "rambulance/exceptions_app"
end

app.config.exceptions_app =
app.config.exceptions_app = ->(env) {
begin
ActiveSupport::Dependencies.load_missing_constant(Object, :ExceptionsApp)
->(env){ ::ExceptionsApp.call(env) }
::ExceptionsApp.call(env)
rescue NameError
::Rambulance::ExceptionsApp
::Rambulance::ExceptionsApp.call(env)
end
}

ActiveSupport.on_load(:after_initialize) do
Rails.application.routes.append do
Expand Down
15 changes: 12 additions & 3 deletions test/fake_app/rails_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ class TestApp < Rails::Application
config.eager_load = false
config.root = File.dirname(__FILE__)
config.autoload_paths += ["#{config.root}/lib"] if ENV["CUSTOM_EXCEPTIONS_APP"]

if Rails::VERSION::STRING >= "5.2"
config.action_controller.default_protect_from_forgery = true
end
end
Rails.backtrace_cleaner.remove_silencers!
Rails.application.initialize!

# routes
Rails.application.routes.draw do
resources :users
resources :projects, only: :index
resources :projects, only: [:index, :create]
end

# custom exception class
Expand Down Expand Up @@ -76,11 +80,16 @@ def edit
end
end
class ProjectsController < ApplicationController
if self.respond_to?(:skip_forgery_protection)
skip_forgery_protection
end

if self.respond_to? :skip_before_action
skip_before_action :bad_filter
skip_before_action :bad_filter, except: :create
else
skip_filter :bad_filter
skip_filter :bad_filter, except: :create
end

def index; end
def create; end
end
7 changes: 7 additions & 0 deletions test/requests/error_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class ErrorJsonTest < ActionDispatch::IntegrationTest
assert_equal "Page not found", json_response['message']
end

test 'renders an error page on a POST request' do
post '/projects.json'

assert_equal 500, response.status
assert_equal "Something went wrong", json_response['message']
end

private

def without_layouts
Expand Down

0 comments on commit d68d677

Please sign in to comment.