samedi 9 juillet 2016

Connecting Rails api association with Ember

I've two models: user and card.

When a user creates a new card, I want to associate the user with that card. (You can assume that the user_id is 1)

For the Rails part,

card.rb

class Card < ActiveRecord::Base
  has_and_belongs_to_many :users
end

user.rb

class User < ActiveRecord::Base
  has_and_belongs_to_many :cards
end

For the Ember part,

app/routes/card/new.js

  actions: {
    save(title, description) {
      let user = this.get('store').find('user', 1);

      const newCard = this.get('store').createRecord('card', {
        title,
        description,
        user
      });
      newCard.save().then((card) => {
        // go to the new item's route after creating it.
        this.transitionTo('card.card', card);
      });
    }
  }

Currently, I can associate them through the Rails console. I want to know how I can associate both of them through the Rails api and Ember data so that when I create a new card, it associates the current user along with that card.

Repo link (Rails): http://ift.tt/29D0mtm

Repo link (Ember): http://ift.tt/1Xv7Dye




Aucun commentaire:

Enregistrer un commentaire