Ember Rails 'POST' Nested Route

Gleb Nazarkin

When saving nested resources (ex saving a highlight that belongs to a property) it seems that Ember doesn't pickup on the nested route hierarchy and 'POSTS' to just 'api/v1/highlights' when i need it to 'POST' to 'api/v1/properties/property_id/highlights'. So I thought about un-nesting the routes on the back-end to solve it and passing the property_id somehow, but it would be a lot easier to just be able to have it understand the correct route. I came across the buildURL mixin to build the correct path for each time and was wondering if that's the best way to do it and is there something I'm doing wrong?

Thanks for all the help in advance

Here are parts of my code...

routes.rb

namespace :api, defaults: { format: :json } do
namespace :v1 do
  resources :users, only: [:show, :update] 
  resources :user_devices, only: [:show]
  resources :properties do

    resources :highlights do
      resources :options
    end
  end 
  resources :fields
end

end

router.js

this.route('properties', function() {
this.route('new');

this.route('property', {path: ':property_id'}, function() {
  this.route('edit');
  this.route('details');

  this.route('highlights', function() {
    this.route('new');

    this.route('highlight', {path: ':highlight_id'}, function() {
      this.route('edit');

      this.route('options', function() {
        this.route('new');

        this.route('option', {path: ':option_id'}, function() {
          this.route('edit');
        });
      });
    });
  });

adapter

import Ember from 'ember';

export default Ember.Mixin.create({
  host: 'http://localhost:3000',
  namespace: 'api/v1'
});

highlight model in highlight.js

property: DS.belongsTo('property', {async: true }),

property model in property.js

highlights: DS.hasMany('highlight', {async: true }),
Billybonks

The general convention when using ember-data is that each models endpoints are siblings of each other.

If you really wanted to do this you would have to build your own adapter, which is much more effort then doing things according to convention.

so your routes.rb would look like

namespace :api, defaults: { format: :json } do
  namespace :v1 do
    resources :users, only: [:show, :update] 
    resources :user_devices, only: [:show]
    resources :properties
    resources :highlights
    resources :options
  end 
  resources :fields
end

models/highlights.js

export default Model.extend({
  property: DS.belongsTo('property', {async: true })
})

models/property.js

export default Model.extend({
  property: DS.hasMany('highlight', {async: true })
})

models/options.js

export default Model.extend({
  property: DS.belongsTo('highlight', {async: true })
})

action in property controller for example

save:function(){
   highlight.save().then(function(highlight){
     property.get(highlights).pushObject(highlight);
     property.save();
   });
}

when property.save executes, your adapter will automatically add highlight_ids :[1] to the payload it sends to the server

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ember dynamic route with nested route inside it

Rails route for nested resource

Ember assets broken in nested route template

Rails route alias for a nested route with specific id

rails custom action in nested route

rails render an action with nested route

Ruby on Rails - Nested Route Collections

Rails: route helpers for nested resources

Rails nested route form issue

How to control rails resource route options in Ember?

rails partial form only build and available for post method, can't build for and use for put method (nested route)

No route matches [POST] in ruby on rails

how to define nested routes + ember not rendering template for nested route

Ember: jQuery Post with nested JSON-Payload

Ember.js / Rails Nested JSON

No POST route - nested resources causing confusion

Overriding parent model loading in nested Ember.js route

How to define a nested route to render when hitting the parent in Ember

Ember: Log model data in template from a nested route

Ember nested route show.hbs params not working

Ember.js - Rendering a different layout on a nested route change

Ember JS and nested routes: the provided route name is invalid

Customize loading template for non-nested ember route

ElasticSearch handling Rails route params with nested objects

Route not being detected for nested resource in Rails 4

Rails nested resources ignore single route

No route matches [POST] "/contacts/new" Ruby on Rails

Update attributes rails - No route matches [POST]

Ruby on Rails - No route matches [POST] "/setor/update"