Rails4で「顧客」を足場にすると
rails g scaffold 顧客名
JSON 応答に「id」が含まれていない
curl http://localhost:3000/customers.json
[{"name":"Test 1","url":"http://localhost:3000/customers/1.json"}]
「id」を追加するにはどうすればよいですか?
Rails4で「顧客」を足場にすると
rails g scaffold 顧客名
JSON 応答に「id」が含まれていない
curl http://localhost:3000/customers.json
[{"name":"Test 1","url":"http://localhost:3000/customers/1.json"}]
「id」を追加するにはどうすればよいですか?
gem jbuilderを使用している場合、ファイルindex.json.jbuilderがviews/customersフォルダーに生成されます
デフォルトではこのように見えるはずです
json.array!(@customers) do |customer|
json.extract! customer, :name
json.url customer_url(customer, format: :json)
end
2 行目に :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"}]