I had a problem trying to save many, many relationships in Ember.js using ember-data and rails data. The association works fine on the ember side, but when I try to complete a transaction, it will not include a list of new associations when sent to the rails. Any help would be greatly appreciated, I pulled out my hair trying to find an example application that makes something so simple on github, but I cannot find it.
Here is my version of my Ember code:
App.Store = DS.Store.extend revision: 11 adapter: DS.RESTAdapter.create url: '/api' App.Router.map -> @resource 'rosters' @resource 'users' App.User = DS.Model.extend rosters: DS.hasMany 'App.Roster' App.Roster = DS.Model.extend users: DS.hasMany 'App.User' App.RostersEditRoute = Ember.Route.extend setupController: (controller, model) -> controller.set 'users_list', App.User.find() App.RostersEditView = Ember.View.extend userCheckbox: Em.Checkbox.extend checkedObserver: ( -> users = @get 'roster.users' if @get 'checked' users.addObject @get 'user' else users.removeObject @get 'user' @get('controller.store').commit() ).observes 'checked'
Change form:
each user in users_list label.checkbox view view.userCheckbox rosterBinding="current_roster" userBinding="user" |
Rails Backend (Using Inherited Resources and Serializer Self-Service):
class User < ActiveRecord::Base has_many :rosters_users has_many :rosters, through: :rosters_users accepts_nested_attributes_for :rosters end class RostersUser < ActiveRecord::Base belongs_to :user belongs_to :roster end class Roster < ActiveRecord::Base has_many :rosters_users has_many :users, through: :rosters_users accepts_nested_attributes_for :users end module Api class RostersController < BaseController end end module Api class UsersController < BaseController end end module Api class BaseController < ActionController::Base inherit_resources respond_to :json before_filter :default_json protected
So, as I said, the association works fine on Ember, but the rails don't seem to get new association data. When I check the XHR tab in the web inspector, I see that he sent this message:
Payload request {"roster": {"created_at": "Thu, Mar 21, 2013 11:16:02 PM GMT", "team_id": "1"}}
And I come back with a content error 204 because nothing has changed.
Mcpants
source share