我正在尝试使用 jbuilder gem 来格式化 json 输出。
控制器
class LocationsController < ApplicationController
def tree
@locations = Location.all
end
tree.json.jbuilder
Jbuilder.encode do |json|
json.id @location.id
json.name @location.name
end
使用 url 进行测试:
http://localhost:5000/locations/tree.json
结果:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Extracted source (around line #2):
1: Jbuilder.encode do |json|
2: json.id @location.id
3: json.name @location.name
4: end