0

I'm starting to use Ember.js with Rails 3.2.13 through the ember-rails gem. I am unable to access data through the browser console with the following command:

var posts = App.Post.find();

It returns undefined on the console, but the following is outputted on the server logs:

Started GET "/posts" for .......
Processing by PostsController#index as JSON
Parameters: {"posts"=>{}}
Post Load SELECT "posts".* FROM "posts"
Completed 200 OK .....

Please let me know if I am doing something wrong or missing something. Thanks


Here are the steps I took to install ember-rails

1) Add ember-rails to gem file

2) After my bundle install, I run:

rails g ember:bootstrap

There are the MVC directories, app.js, application.js, router.js, and store.js.

store.js has the following contents:

App.Store = DS.Store.extend({
  revision: 11
});

3) Then I did

rails g scaffold Post title:string body:text

Next I added the active_model_serializer gem and generated a serializer for Post.

Pan Wangperawong
  • 1,250
  • 2
  • 13
  • 29
  • `App.Post.find();` is async so I guess It's normal that you are getting `undefined` instantly back from the browser console, if you open your network tab (assuming chrome) you should see the xhr request being made, click on it and then go to the sub tab `Response` and look if there is your data. If it's there, than everything is working correctly. Now, why are you getting undefined in the console? Well I guess since the console is not async :) and is not waiting for the response to come back you get undefined, does it make sense? – intuitivepixel May 18 '13 at 23:33
  • I understand the async part, but based on this [tutorial](http://www.zhubert.com/blog/2012/04/28/ember-dot-js-and-rails-part-2/) how are they are able to access the data? My steps are pretty similar, so what am I not doing? - thanks – Pan Wangperawong May 19 '13 at 05:21
  • well the tutorial you are using is more then one year old ... a lot has changed since then in ember land, you should maybe use a different more up to date tutorial. – intuitivepixel May 19 '13 at 08:41

1 Answers1

0

Can you check the actual response from the server (in Chrome, open inspector, find the request in the network panel and look at the response)? Make sure that the server actually is returning the correct JSON. (Also obviously, I'm assuming you've already created some Post models on the Rails server).

Next, make sure you have the proper attributes listed under your serializer; I think by default it only includes the id.

You can also try passing an id to find: App.Post.find(1) and again see what the server response is.

App.Post.find() is async but it always returns a promise for data -- it should never be returning undefined. You can call .then() on the model promise and pass it a function expecting the loaded model as an argument to be run as soon as the promise resolves.

Hope this helps. Try checking the server response and let me know if any of this is unclear.

Sherwin Yu
  • 3,180
  • 2
  • 25
  • 41