0
我試圖使用jbuilder gem來格式化json輸出。rails jbuilder被調用id爲零
控制器
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
我試過這個改變。我最終得到了結果= {} – Reddirt 2013-02-13 14:25:24
我刪除了第一行和最後一行,現在它可以工作。謝謝 – Reddirt 2013-02-13 14:36:38