0

I have a Rails app that has both HTML and JSON endpoints, and the JSON is being used by an iOS app. The iOS developer would like all error messages output to be changed from something like {"errors":{"email":["has already been taken"],"password":["is too short (minimum is 6 characters)"]}} to the form { "error" : "message"}, but there doesn't seem to be a good place to do a single fix/formatting.

My feeling is there are 3 different steps here:

  1. Modifying error responses for JSON responses only
  2. Calling .full_messages on all errors
  3. Standardizing keys so errors or other variations become simply error

My questions:

  1. Is there a place to set the error format to full_messages for all JSON messages or does it need to be spliced into all error calls?
  2. Is there a place to standardize keys for all errors?
    • I guess I could reopen as_json in ActiveRecord::Base, but there may be other errors not coming from AR...

Potential items

These are potential solutions that have been mentioned so far.

  1. Presenters
    • This seems useful for aggregating information from multiple models but still requires that the new Class be called in each method.
    • I was hoping for a higher-level solution that caught output and reformatted it before passing it along without having to manually insert it throughout the app.
4

1 に答える 1

0

おそらく遅い答えですが、それがより多くの視点を追加するのに役立つ場合.

RABL を json プレゼンテーション レイヤーとして使用し、それを使用して json エラーを ios に出力しようとしました。

RABL のコード例:

object @submission

child :errors do |e|
  e.full_messages.each do |message|
   node(:message){message}
  end
 end

現在、NSError *error オブジェクトをログに記録すると、期待どおり UserInfo ディクショナリに次のように表示されます。 0 バイトから 153600 バイトの間"}}]}}

UserInfo ディクショナリの値が、ios ディクショナリに適した形式を返さないことがわかります。'=' は代わりに ':' であり、配列には '(' と ')' の代わりに '[' があります。

NSString にカテゴリを追加して、返されたオブジェクトのこれらの非辞書要素をすべて NSDictionary に適したテキスト形式に置き換えてから、ios NSDictionary にキャストし、標準のオブジェクト アクセス メソッドを使用することをお勧めしますか? これが正しいアプローチなのか、それとももっとエレガントな方法があるのか​​ 疑問に思っていますか? ありがとうございました。

于 2013-05-17T16:34:18.970 に答える