Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4838

How to deal with backend nested routes

$
0
0

@belgoros wrote:

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 ? Thank you.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4838

Trending Articles