Help us understand the problem. What is going on with this article?

Padrino 0.11 以降で Better Errors を使う

More than 5 years have passed since last update.

Padrino 0.12.0 以降では protect_from_csrf に except オプションを指定します。

app/app.rb
module Sample
  class App < Padrino::Application
    #...
    set :protect_from_csrf, except: %r{/__better_errors/\d+/\w+\z}

以降は、Padrino 0.11.x でのお話です。


Better Errors の導入自体は以前書いた記事の通りでいいのですが、protect_from_csrf が有効となっている場合 ( Padrino 0.11 からデフォルトで有効 ) には、Better Errors が表示するページからの HXR が通らずページ右側が表示されません。

これでは Better Errors を使っている意味がまるっきり無くなってしまうので、とりあえず、以下のパッチで回避しました。

1.指定したパスを保護対象外とするパッチを lib の下に置く

lib/rack_protection.rb
if Padrino.env == :development
  module Rack
    module Protection
      class AuthenticityToken
        def accepts_with_allowed_path?(env)
          if defined?(Padrino::Application.exclude_from_protection)
            [Padrino::Application.exclude_from_protection].flatten.each do |path|
              return true if Regexp.new(path).match(env['REQUEST_PATH'])
            end
          end
          accepts_without_allowed_path?(env)
        end
        alias_method_chain :accepts?, :allowed_path
      end
    end
  end
end 

2.保護対象外とするパスを Better Errors の設定と一緒に指定する

config/boot.rb
if Padrino.env == :development
  # Setup better_errors
  Padrino::Application.use BetterErrors::Middleware
  BetterErrors.application_root = PADRINO_ROOT
  BetterErrors::Middleware.allow_ip! ENV['TRUSTED_IP'] if ENV['TRUSTED_IP']
  Padrino::Application.set :exclude_from_protection, '/__better_errors' # Append this
  # ...
end

Rack:: Protection 側をいじるのは嫌だったのですが、これしか思いつきませんでした…。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away