使用Rails Ember.js accepts_nested_attributes_for和多态的has_many / belongs_to的 [英] Ember.js with Rails accepts_nested_attributes_for and polymorphic has_many/belongs_to

查看:134
本文介绍了使用Rails Ember.js accepts_nested_attributes_for和多态的has_many / belongs_to的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails的API一边我有以下2种型号:

On the Rails API side I have the following 2 models:

class Grower < ActiveRecord::Base
 has_many :addresses, as: :addressable
 accepts_nested_attributes_for :addresses
end

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true
end

以及一个种植者控制器返回并可以创建带有嵌入地址/更新种植者属性。我也有正确的路由,这样的地址可以查看/创建/更新为特定的种植者地址控制器。后者是更多的是在情况,我不知道我会需要返回/更新/创建地址作为一个单独的有效载荷。

as well as a Growers controller which returns and can create/update Growers with embedded Addresses attributes. I also have an Addresses controller with proper routing so that Addresses can be viewed/created/updated for a specific Grower. The latter is more of an "in-case", and I'm not sure I'll be needing to return/update/create addresses as a separate payload.

我开始尝试和拼凑灰烬应用程序,可以让我查看/编辑/在同一时间作为其地址(ES)创建一个种植者。任何人都可以点我到一个现有的真实或示例应用程序,这是否?我将张贴我的code作为我走,但是我已经在那里我将运行到麻烦一些地区的一个想法:

I am starting to try and piece together an Ember app that would allow me to view/edit/create a Grower at the same time as its Address(es). Can anyone point me to an existing real or sample app that does this? I will be posting my code as I go along, but I already have an idea of some areas where I will be running into trouble:


  1. Rails的回报/预计,嵌套PARAMS名为 addresses_attributes 。灰烬,我确信,不使用该约定。什么是解决这个的最好的方法?

  1. Rails returns/expects nested params named addresses_attributes. Ember, I am sure, doesn't use that convention. What's the best approach of resolving this?

由于多态性协会(对象比蒜农其他可寻址),对API /地址的一面,以获得正确的belongs_to的对象,Rails使用的 addressable_id 连同<强> addressable_type 。在这个例子中addressable_type将是种植者,并且addressable_id将是grower_id值。如何将一个去翻译上的灰烬一边?

Because of the polymorphic association (objects other than Grower can be addressable), on the API/Address side, to get the correct belongs_to object, Rails uses addressable_id in conjunction with addressable_type. In this example the addressable_type would be "Grower", and the addressable_id would be the grower_id value. How would one go about translating that on the Ember side?

更新:

我得到它的工作至少有几个不同的方式。我的preferred解决方案,至少在这个特定的情况下,在应答部分。

I got it to work at least a couple different ways. My preferred solution, at least for this particular case, is in the answer section.

推荐答案

我发现一对夫妇的方式来完成它,但最终的办法并不需要在导轨上/ API方的任何更改。

I found a couple ways to get it done, but the final approach doesn't require any changes on the Rails/API side.

在客户端(恩伯)侧:


  1. 我添加在地址的hasMany属性的App.Grower模式。我也映射它放在RESTAdapter从API什么期望的,通过设置在作为的地址的为 addresses_attributes

  2. 我添加在种植者(现在 - 将改变为​​寻址一旦我有其他的寻址的型号)上App.Address财产的belongsTo。这不是真的需要我做什么,但它可能在将来有用。

  3. 我设置的地址的的 RESTAdapter 嵌入式:总是

  4. App.GrowersEditController ,我只是做一个的 model.save 的(器transaction.commit 的),而子地址被自动保存通过API调用。

  5. 在App.GrowersAddController,我使用 App.Address.createRecord App.Grower.createRecord 使用用户输入的种植者和地址数据的方法。然后我使用 pushObject 方式追加地址的种植者,然后调用的保存的对种植者(提交的上交易)。同样,地址数据被提交,并自动保存。

  1. I added the addresses hasMany property to the App.Grower model. I also mapped it on the RESTAdapter to what's expected from the API, by setting the key for addresses to addresses_attributes.
  2. I added the grower (for now - will change to addressable once I have other addressable models) belongsTo property on App.Address. It's not really required for what I'm doing, but it might be useful in the future.
  3. I set the addresses on the RESTAdapter to be embedded: 'always'.
  4. On the App.GrowersEditController, I just do a model.save (transaction.commit), and the child addresses are automatically saved via the API call.
  5. On the App.GrowersAddController, I use the App.Address.createRecord and App.Grower.createRecord methods using the user-entered Grower and Address data. Then I use the pushObject method to append the Address to the Grower, and then call a save on the Grower (commit on transaction). Again, the address data gets submitted and saved automatically.

这篇关于使用Rails Ember.js accepts_nested_attributes_for和多态的has_many / belongs_to的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆