1
votes

I have a rails api with the "active_model_serializers" gem.

http://localhost:3000/api/buildings/30.json generates for me this:

{  
   building:{  
      id:30,
      city_name:"msc",
      infrastructure:[  
         {  
            id:40,
            name:"name 1",
            created_at:"2015-07-30T08:26:49.000Z",
            updated_at:"2015-07-30T08:26:49.000Z"
         },
         {  
            id:69,
            name:"name 2",
            created_at:"2015-07-30T08:26:50.000Z",
            updated_at:"2015-07-30T08:26:50.000Z"
         },
         {  
            id:39,
            name:"name 3",
            created_at:"2015-07-30T08:26:49.000Z",
            updated_at:"2015-07-30T08:26:49.000Z"
         }
      ]
   }
}

Also I use EmberJs with ActiveModelAdapter. But Ember expects this:

{  
   building:{  
      id:30,
      city_name:"msc",
      infrastructure:[  
         40,
         69,
         39
      ]
   },
   infrastructure:[  
      {  
         id:40,
         name:"name 1",
         created_at:"2015-07-30T08:26:49.000Z",
         updated_at:"2015-07-30T08:26:49.000Z"
      },
      {  
         id:69,
         name:"name 2",
         created_at:"2015-07-30T08:26:50.000Z",
         updated_at:"2015-07-30T08:26:50.000Z"
      },
      {  
         id:39,
         name:"name 3",
         created_at:"2015-07-30T08:26:49.000Z",
         updated_at:"2015-07-30T08:26:49.000Z"
      }
   ]
}

How can I make ember working with rails json structure? Thx!

2

2 Answers

1
votes

You need to use the Embedded Records Mixin on the Ember side.

App.ColorSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    foos: {embedded: 'always'}
  }
});

Check out this answer

0
votes

Another option is to go the JSON API route.

I think it's a better option because

  • you start coding for the current/future format of Ember Data
  • it's clearer to map and understand what happens to data

I wrote a guide that is at http://emberigniter.com/modern-bridge-ember-and-rails-5-with-json-api/ (for Rails 5) but you surely can adapt it to your version of Rails