0
0

More than 1 year has passed since last update.

【Jbuilder】うまくJSONを返せないときに確かめること

Posted at

その1:名前一致しているか

ファイル名やルーティングが一致してないとエラーが出たり、JSON表示できない。
特に2回くらい複数形s忘れしてるのでもう忘れない。

routes.rb
namespace :api, format: 'json' do
  namespace :v1 do
    resources :post_infos, only: [:index]
  end
end
app/views/api/v1/post_infos/index.json.jbuilder
json.result 'success'
json.post_infos do
  json.date @post_infos[:date]
  json.genre @post_infos[:genre]
  json.like_count @post_infos[:like_count]
end

その2:APIアプリケーションコントローラーを継承しているか

app/controllers/api/v1/post_infos_controller.rb
class Api::V1::PostInfosController < Api::ApplicationController
  def index
    ...
  end
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0