What is the best way to go on Ember side to deal with nested routes defined on backend side (Rails API) ? In a Rails app there are the nested routes defined as follows:
#routes.rb
resources :languages, only: :index
resources :shops do
resources :shop_languages, path: '/languages'
end
on the Ember side the routes are defined as follows:
# router.js
Router.map(function() {
...
this.route('languages', { path: 'shops/:shopId/languages'});
});
How can I call create/delete actions for a shop language in this case ?
I have the action defined on save
button in Ember template:
# templates/languages.hbs
<button type="button" class="btn btn-success btn-sm" onclick={{action "saveLanguage" aLanguage}}>
and the action defined in the controller:
# controllers/languages.js
export default Controller.extend({
...
actions: {
saveLanguage(aLanguage) {
var shopLanguage = this.store.createRecord('shop-language', {
language: aLanguage,
shop: this.get('currentShop.shop'),
modifiedBy: this.get('currentUser.user').get('username')
});
shopLanguage.save().then(function() {
this.get('flashMessages').info('New language added with success');
});
}
}
In this case I have an error:
Started POST "/shop-languages" for 127.0.0.1 at 2018-04-03 11:58:08 +0200
ActionController::RoutingError (No route matches [POST] "/shop-languages"):
because Ember tried to POST at shop-languages
route instead of shop/:shop_id/languages
.
Here the list of routes in Rails:
shop_shop_languages GET /shops/:shop_id/languages(.:format) shop_languages#index
POST /shops/:shop_id/languages(.:format) shop_languages#create
shop_shop_language GET /shops/:shop_id/languages/:id(.:format) shop_languages#show
PATCH /shops/:shop_id/languages/:id(.:format) shop_languages#update
PUT /shops/:shop_id/languages/:id(.:format) shop_languages#update
DELETE /shops/:shop_id/languages/:id(.:format) shop_languages#destroy
Any idea ? Should I create an adapter to be able to create/delete a language for a shop ? By the way I already have one defined:
#adapters/shop-language.js
export default ApplicationAdapter.extend({
urlForQuery (query/*, modelName*/) {
return `/shops/${query.shopId}/languages`;
}
});
Or should I use nested routes on the Ember side. What is better ? Thank you.