29

jbuilder.json テンプレートで常に次のようなコードを記述するのは面倒です。

json.extract! notification, :id, :user_id, :notice_type, :message, :resource_type, :resource_id, :unread, :created_at, :updated_at

だから私はこのようにコーディングしたいと思います。

json.extract_all! notification

次のコードのようにできることがわかりましたが、それでも少し長いです。

notification.attributes.each do |key, value|
  json.set!(key, value)
end

もっと良い方法はありますか?

4

6 に答える 6

11

jbuilder 1.5.0 とマージを使用しています! うまくいきませんでしたが、別の構文を見つけました:

json.(notification, *notification.attributes.keys)
于 2014-10-14T13:36:42.193 に答える
0

あなたは見るかもしれませんjson.except!

json.except! @resource, :id, :updated_at

json.except! @resource

https://github.com/chenqingspring/jbuilder-except

于 2017-08-21T07:14:45.507 に答える