jeudi 22 mars 2018

Call a nested Rails route from Ember App

How to call a nested Rails route from Ember app if I don't need nested templates ? I have the following routes in Rails router:

# routes.rb
resources :shops do
  resources :shop_languages
end

So to get a list of shop languages the shops/:shop_id/shop_languages should be hit.

Here is ShopsLanguagesController:

# controllers/shop_languages_controller.rb
class ShopLanguagesController < ApplicationController
  before_action :find_shop

  def index
    json_response @shop.shop_languages, :ok, include: 'language'
  end

  private

    def find_shop
      @shop = Shop.find(params[:shop_id])
    end
end

In Ember app I have the routes defined as follows:

# router.js
Router.map(function() {
...
  this.route('languages', { path: '/shops/:shop_id/shop_languages'});
});

In Ember application.hbs template the languages link is defined as follows

# application.hbs

..


In Ember languages.js route handler, I'm trying to load shop languages:

# routes/languages.js
model(params) {
  return this.store.query('shop-language', { shop_id: params.shop_id })
}

Ember hits /shop-languages end-point instead of the nested one shops/:shop_id/shop_languages.

Of course, I've defined the corresponding models on Ember side:

# models/shop-language.js
import DS from 'ember-data';

export default DS.Model.extend({
  shop: DS.belongsTo('shop'),
  language: DS.belongsTo('language'),
  modified_by:  DS.attr('string')
});

What is wrong with that and how to get it work? Thank you




Aucun commentaire:

Enregistrer un commentaire