如何使用Rails Jbuilder提取所有属性?

chi*_*ram 25 ruby-on-rails jbuilder

在jbuilder.json模板中一直编写这样的代码是一件痛苦的事:

json.extract! notification, :id, :user_id, :notice_type, :message, :resource_type, :resource_id, :unread, :created_at, :updated_at
Run Code Online (Sandbox Code Playgroud)

所以我想像这样编码;

json.extract_all! notification
Run Code Online (Sandbox Code Playgroud)

我发现我可以像下面的代码那样做,但它们对我来说仍然有点冗长.

notification.attributes.each do |key, value|
  json.set!(key, value)
end
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法?

jva*_*nen 9

我正在使用jbuilder 1.5.0并合并!没用,但我找到了另一种语法:

json.(notification, *notification.attributes.keys)
Run Code Online (Sandbox Code Playgroud)