简体   繁体   中英

Can't Retrieve Model Data

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM