mmag

ハマったことメモなど

EmberでError: No model was found for '0'

なんかエラーでた。

RestAdapterを使っているときで、バックエンドはRailsでした。

バックエンドがjbuilderjson作って返しているのですが、そのjsonの形式が原因でした。

scaffoldしっぱなしだと、

# app/views/posts/index.json.jbuilder

json.array!(@posts) do |post|
  json.extract! post, :id, :body
  json.url post_url(post, format: :json)
end

こんなんです。これで生成されるjsonはデカイ配列です。

[
  post1,
  post2,
  post3,
   :
]

しかし、emberのRESTAdapterが期待しているのは、

{
  posts: [
    post1,
    post2,
    post3,
     :
  ]
}

こういう感じなんだそうです。

なので、

# app/views/posts/index.json.jbuilder

json.set! :posts do
  json.array!(@posts) do |post|
    json.extract! post, :id, :body
    json.url post_url(post, format: :json)
  end
end

てな風に囲んでやると動きました。