Ember Rails 'POST' 嵌套路由

Ember Rails 'POST' Nested Route

保存嵌套资源时(例如保存属于 属性 的高亮显示)似乎 Ember 不会在嵌套的路由层次结构上拾取,'POSTS' 只是 'api/v1/highlights' 当我需要它时 'POST' 到 'api/v1/properties/property_id/highlights'。所以我想过在后端取消嵌套路由来解决它并以某种方式传递 property_id ,但是让它理解正确的路由会容易得多。我遇到了 buildURL mixin 每次都构建正确的路径,我想知道这是否是最好的方法,我做错了什么吗?

提前感谢大家的帮助

这是我的部分代码...

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

结束

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');
        });
      });
    });
  });

适配器

import Ember from 'ember';

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

在 highlight.js

中突出显示模型
property: DS.belongsTo('property', {async: true }),

属性 模型 property.js

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

使用 ember-data 时的一般约定是每个模型端点都是彼此的兄弟姐妹。

如果您真的想这样做,则必须构建自己的适配器,这比按照惯例做事要费力得多。

所以你的routes.rb看起来像

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 })
})

例如属性控制器中的动作

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

当 property.save 执行时,您的适配器会自动将 highlight_ids :[1] 添加到它发送到服务器的负载中