轨道4和x27中的脚手架;在JSON响应中不包含id

轨道4和x27中的脚手架;在JSON响应中不包含id,json,response,ruby-on-rails-4,Json,Response,Ruby On Rails 4,当我将“客户”放在栏杆上时4 rails g脚手架客户名称 JSON响应不包含“id” 如何添加“id”如果您使用的是gemjbuilder文件index.json。jbuilder在views/customers文件夹中生成 默认情况下应该是这样的 json.array!(@customers) do |customer| json.extract! customer, :name json.url customer_url(customer, format: :json) end

当我将“客户”放在栏杆上时4

rails g脚手架客户名称

JSON响应不包含“id”


如何添加“id”

如果您使用的是gemjbuilder文件index.json。jbuilder在views/customers文件夹中生成

默认情况下应该是这样的

json.array!(@customers) do |customer|
  json.extract! customer, :name
  json.url customer_url(customer, format: :json)
end
只需在第二行中添加:id即可在json响应中看到它

json.array!(@customers) do |customer|
  json.extract! customer, :name, :id
  json.url customer_url(customer, format: :json)
end
新的json响应将是

[{"name":"Test 1","id":1,"url":"http://localhost:3000/customers/1.json"}]

遇到了完全相同的问题。谢谢你的自传!谢谢@moeso,我很高兴能帮上忙:)
[{"name":"Test 1","id":1,"url":"http://localhost:3000/customers/1.json"}]