简体   繁体   English

Ember.js / Rails将关系发布到Rails

[英]Ember.js / Rails Posting Relationships to Rails

Ok, I'm working on a simple Ember / Rails app, and I'm trying to post a hasMany association in a single HTTP request. 好的,我正在开发一个简单的Ember / Rails应用程序,并且试图在单个HTTP请求中发布hasMany关联。 For what it's worth, I'm using Rails for the API. 对于它的价值,我将Rails用于API。

  • Ember: 1.4.0-beta.1+canary.011b67b8 灰烬:1.4.0-beta.1 + canary.011b67b8

  • Ember Data: 1.0.0-beta.5+canary.d9ce2a53 灰烬数据:1.0.0-beta.5 + canary.d9ce2a53

  • Handlebars: 1.1.1 车把:1.1.1

  • jQuery: 1.10.2 的jQuery:1.10.2

Release Model 发布模型

App.Release = DS.Model.extend
  name: DS.attr 'string'
  tracks: DS.hasMany 'track', {embedded: 'true'}

A "Release" has many "Tracks" 一个“发行”有很多“曲目”

App.ReleasesNewRoute = Ember.Route.extend

  model: -> @store.createRecord 'release'

  afterModel: (release, transition)->
    release.get('tracks').addObject(@store.createRecord 'track',{
      name: 'Track 1'  
    }).pushObject(@store.createRecord 'track',{
      name: 'Track 2'  
    })

  setupController: (controller, model)->
    controller.set('content', model)

Releases Controller 发布控制器

App.ReleasesNewController = Ember.ObjectController.extend

  actions:{
    save: ->
      @content.save()
  }

How can I post the two tracks and one release at once? 如何同时发布两个曲目和一个发行版? I'm planning to use accepts_nested_attributes_for :tracks in my Rails API... but I'll be happy if I can see the Tracks in my development console for a start. 我打算在Rails API中使用accepts_nested_attributes_for:tracks ...,但是如果我能在开发控制台中看到Tracks作为开始,我会很高兴。

I got this working like this: 我得到这样的工作:

App.ReleaseSerializer = DS.RESTSerializer.extend

  serializeHasMany: (record, json, relationship)->  
    key = relationship.key
    hasManyRecords = Ember.get(record, key)

    key = key+'_attributes'

    if hasManyRecords && relationship.options.embedded == 'true'
      json[key] = []
      hasManyRecords.forEach (item, index)->
        json[key].push(item.serialize())
    else
      @._super(record, json, relationship)

This works nicely with :accepts_nested_attributes_for in Rails. 这与Rails中的:accepts_nested_attributes_for很好地配合。 Rails is expecting a hash of track models nested under the release as tracks_attributes . Rails期望在发行版下面嵌套的轨道模型的哈希为tracks_attributes

Couple gotchas: 夫妻陷阱:

  • Rails won't use the create action for the Child model, you'll have to put anything you wanna do to the children models in your Parent's create action. Rails不会对Child模型使用create动作,您必须将要对孩子模型做的任何事情放在Parent的create动作中。

  • Remember to whitelist your children's parameters for Rails 4. This looks like: 记住将孩子的Rails 4参数列入白名单。这看起来像:

      def release_params params.require(:release).permit(:name, tracks_attributes: [:name]) end 

'embedded' is not supported anymore in ember-data 1 beta. ember-data 1 beta版不再支持“嵌入”。 check this link to see how to create child records with using embed. 检查此链接以了解如何使用embed创建子记录。 Better to use 更好用

https://github.com/emberjs/data/blob/master/TRANSITION.md#embedded-records https://github.com/emberjs/data/blob/master/TRANSITION.md#embedded-records

check this question to know more about creating child records in a different and better way How to Add Child Record to Existing Parent Record? 检查此问题,以了解更多有关以其他更好的方式创建子记录的信息。 如何将子记录添加到现有的父记录中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM