Ember.js with Rails accepts_nested_attributes_for and polymorphic has_many / belongs to

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 the Growers controller, which returns and can create / update Growers with the built-in attributes of Addresses. I also have an Address controller with the correct routing so Addresses can be viewed / created / updated for a specific Grower. The latter is more likely an "in-case", and I'm not sure that I will need to return / update / create addresses as a separate payload.

I'm starting to try to combine the Ember application, which will allow me to view / edit / create Grower simultaneously with its address (addresses). Can someone point me to an existing real or sample application that does this? I will publish my code while I go, but I already have an idea of ​​some areas where I will encounter difficulties:

  • Rails returns / expects nested parameters called addresses_attributes . Amber, I'm sure, is not using this convention. What is the best approach to resolving this?

  • Due to the polymorphic association (objects other than Grower can be addressable), on the API / Address side, Rails uses addressable_id in combination with addressable_type to get the correct assign_to object. In this example, addressable_type will be "Grower", and addressable_id will be the value of grower_id. How can I translate this on the Ember side?

UPDATE:

I have earned at least a couple of different ways. My preferred solution, at least for this particular case, is in the answer section.

+4
source share
2 answers

I found a couple of ways to do this, but the final approach does not require any changes on the Rails / API side.

On the client side (Ember):

  • I added the hasMany address property to the App.Grower model. I also matched it in the RESTAdapter with what I expected from the API, setting the key for addresses_attributes .
  • I added grower (at the moment - will change to addressable when I have other addressable models) belongs to the App.Address property. It really is not required for what I am doing, but it may be useful in the future.
  • I set the addresses in the RESTAdapter as embedded: 'always' .
  • In App.GrowersEditController, I just make a model.save file (transaction.commit), and the child addresses are automatically saved via an API call.
  • In the App.GrowersAddController application, I use the App.Address.createRecord and App.Grower.createRecord methods , using the user-entered information about Grower and Address. Then I use the pushObject method to add the address to Grower, and then call the save in Grower (commit transaction). Again, address data is transferred and stored automatically.
+6
source

Here is an example strategy based on @ yui code that worked well for me:

 App.Post = DS.Model.extend comments: DS.hasMany('comment') App.PostSerializer = DS.ActiveModelSerializer.extend( DS.EmbeddedRecordsMixin, attrs: comments: {embedded: 'always'} keyForAttribute: (attr) -> if attr == "comments" "comments_attributes" else @_super(attr) ) 

This solution worked well with Ember 1.6.1 and Ember Data 1.0.0-beta.8.2a68c63a .

+7
source

Source: https://habr.com/ru/post/1489327/


All Articles