Skip to content Skip to sidebar Skip to footer

Migrate Routes From Rails To Ember With Existing Rails Application Using "ember-rails"

Using gem 'ember-rails' for an existing rails app. I'm attempting to route one resource using Ember, and I've been told by a number of people that this code should work, but it's n

Solution 1:

If you are using the ember data with rest adapter the configuration is the following:

Given this url your-host/api/v1/newslinks with the following json structure:

{
  newslinks: [
    {id: 1, name: 'foo'},
    {id: 2, name: 'bar' }
  ]
}

You just need to map the newslinks routing:

App.Router.map(function() {
    this.resource('newslinks', { path: '/' });
});

And map the namespace in DS.RestAdapter:

DS.RESTAdapter.reopen({
  namespace: 'api/v1'
});

Here is a live demo using rest adapter and mocking the response.

By default rails will serve the json without the root path of the json:

[
  {id: 1, name: 'foo'},
  {id: 2, name: 'bar' }
]

To get this work easily, in a rails controller just add the :json to your render method, followed by the data. So rails will use the active model serializers, and the root path will be present:

defindex@users = User.all
  render json:@usersend

I hope it helps.

Post a Comment for "Migrate Routes From Rails To Ember With Existing Rails Application Using "ember-rails""