在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
有更好的方法吗?
json.extract! notification, *notification.attributes.keys
但如果 Jbuilder 有类似的方法,我宁愿使用它。 - chikaram