如何使用Rails Jbuilder提取所有属性? [英] How to extract all attributes with Rails Jbuilder?
本文介绍了如何使用Rails Jbuilder提取所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一直在jbuilder.json模板中编写这样的代码很痛苦:
It's a pain to write codes like this all the time in jbuilder.json template:
json.extract! notification, :id, :user_id, :notice_type, :message, :resource_type, :resource_id, :unread, :created_at, :updated_at
所以我想这样编码;
json.extract_all! notification
我发现我可以像下面的代码一样来做,但是它们对我来说仍然有些冗长.
I've found I can do it like the following codes, but they are still a bit lengthy to me.
notification.attributes.each do |key, value|
json.set!(key, value)
end
还有什么更好的方法吗?
Is there any better way?
推荐答案
也许您可以使用json.merge!
.
json.merge! notification.attributes
https://github.com/rails/jbuilder/blob/master/lib/jbuilder.rb#L277
这篇关于如何使用Rails Jbuilder提取所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文