我有这个型号:
class User < ActiveRecord::Base
TYPE = {:admin => "Administrator", :owner => "Owner", :client => "Customer"}
# some codes here
end
当我在rails console中运行以下代码时:
User::TYPE
输出:
{:admin => "Administrator", :owner => "owner", :client => "Customer"}
当我在控制器中添加代码时:
User::TYPE
这是输出:
[
{
"user_types": {
"user": "Administrator",
"owner": "owner",
"client": "Customer"
}
},
{
"user_types": {
"user": "Administrator",
"owner": "owner",
"client": "Customer"
}
},
{
"user_types": {
"user": "Administrator",
"owner": "owner",
"client": "Customer"
}
}
]
我只需要:
{
"user_types": {
"user": "Administrator",
"owner": "owner",
"client": "Customer"
}
我在这里做错了什么。我也试过User::TYPE
,但我没有得到正确的结果。
更新:
这是我的控制器代码:
def user_types
@user_types = User::TYPE
end
在我看来显示结果时,我用rabl来展示json:
object @user_types => false
node(:user_types) {@user_types}
这是输出:
[
{
"user_types": {
"user": "Administrator",
"owner": "owner",
"client": "Customer"
}
},
{
"user_types": {
"user": "Administrator",
"owner": "owner",
"client": "Customer"
}
},
{
"user_types": {
"user": "Administrator",
"owner": "owner",
"client": "Customer"
}
}
]
我只想这样输出:
{
"user_types": {
"user": "Administrator",
"owner": "owner",
"client": "Customer"
}
}
答案 0 :(得分:2)
Rabl正在映射@user_types
。
在你的情况下,它应该是:
# xxxx.rabl
object false
node(:user_types) {@user_types}