0
votes

I have a Rails api that I want to render to an Ember application. I have a question model that looks like

import DS from 'ember-data';

export default DS.Model.extend({
  body: DS.attr('string'),
  response: DS.attr('string')
});

The api can render a bunch of questions on the api/v1/questions.json url. I want to grab this data to use in ember. In my route folder I have namespaced index and random. My random route returns a random question and works well when I go to the questions/random route:

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.findRecord('question', 'random.json');
  }
});

In the terminal where I'm running my Rails server, app starts a GET request as

Started GET "/api/v1/questions/random.json" for 127.0.0.1

I've set up my index route in a similar fashion, but this time I'm returning a collection of questions

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.findAll('question', 'questions.json');
  }
});

In the terminal where I'm running my Rails server, the call goes in as

Started GET "/api/v1/questions" for 127.0.0.1

I'm expecting it to be /api/v1/questions.json but it isn't. No data is being fetched. I've taken a look on the ember docs and tried variations on findRecord, peekAll, etc. The data that I'm requesting (or want to be returned to me) is a collection of question objects but not all of the objects from the api.

I'm wondering why the .json portion that I've specified is not being sent along in my api call.

1
if you have control over the API, then I would request you to change URL to api/v1/questions that's ember-data convention. and this.store.findAll('question') is enough. you don't need to pass an argument in your case. findRecord second argument is an id to search.Ember Freak

1 Answers

1
votes

The findRecord method accepts parameters for model name and the id of the object to retrieve from the API. It then combines these to construct a url so when you call:

this.store.findRecord('question', 'random.json')

'question' is pluralized in the url and 'random.json' is appended resulting in the get request you are seeing:

Started GET "/api/v1/questions/random.json" for 127.0.0.1

The findAll method however only accepts a model name (since it's getting all the objects of that model an id is unnecessary) so when you call:

this.store.findAll('question', 'questions.json');

it simply pluralizes the model name and ignores the second argument resulting in the get request:

Started GET "/api/v1/questions" for 127.0.0.1

This follows common RESTful principles for URLs. If you want to customise how Ember constructs URLs for specific models or all models you should have a look at the DS.Adapter class.

If you want to just be able to query for a subset of records you should also have a look at the store's query method.

Edit: Since you're using DS.RESTAdapter you can override urlForFindAll, or any of the other urlFor methods, to customise the url path.