バックグラウンドで処理される Web サーバー (Rails) にユーザーがデータを送信する状況があります。
POST /fibonacci/6012 HTTP/1.1
Host: example.com
サーバーは、ユーザーがステータスを確認するために使用できるバックグラウンド ジョブへのリンクで応答します。
HTTP/1.1 202 Accepted
Location: http://example.com/jobs/5699121
注意すべき重要なことは、すべての(承認された) ユーザーがジョブのステータスを確認できることです。これは、エラー メッセージをワーカーから Web サーバーに象徴的に伝える必要があることを意味します。ActiveModel エラーに対してこれを行う方法がわかりません。例えば:
class FibonacciCalculation
include ActiveModel::Validations
attr_reader :input
validates_presence_of :input
validates_inclusion_of :input, :in => 0..10_000,
:allow_blank => true
def initialize(params = {})
@input = params[:input]
end
def output
# do fibonacci calculation
end
end
でそのようなオブジェクトを作成FibonacciCalculation.new(:input => -5)してからエラーを取得すると、ActiveModel::Errorsオブジェクトを取得できますが、それをシリアル化する方法がわかりません。を求めると、すでに翻訳されているerrors[:input]私["can't be blank"]またはが得られます。["is not included in the list"]同様に、errors.as_json次のようなものを返します
`{ "errors" => [ "can't be blank" ] }`
またはのようなものを取得するにはどうすればよいです{ :input => [:blank] }か{ :input => [:inclusion] }?