Core: Route.resource().addCollection is not calling method in controller

Created on 19 Feb 2017  路  4Comments  路  Source: adonisjs/core

i currently have this setup in my routes file

// routes.js
Route.group('dashboard', () => {
  Route.get('/', 'DashboardController.index')

  Route.resource('blog', 'BlogController')
    .addCollection('posts')
}).middleware('auth')

however when visiting /blog/posts no response is returned and the request times out.

i've logged the output of the the collection and it seems to be correct:
image

alternatively i've tried manually using a callback, e.g. `Route.resource().addCollection([path], [verb], [cb]) and had no luck with that either.

for reference my BlogController looks like this

'use strict'

class BlogController {
  /* ... */

  * posts (request, response) {
    // tried logging from here, nothing.
    yield response.sendView('blog/posts')
  }  

  /* ... */
}

Most helpful comment

When you create a resource, your resource routes are on the top and collections are on the bottom inside an array. So blog/:id takes precedence over /blog/posts, which is wrong.

I will fix the same by adding to the start of the array instead of pushing collections to the array

All 4 comments

When you create a resource, your resource routes are on the top and collections are on the bottom inside an array. So blog/:id takes precedence over /blog/posts, which is wrong.

I will fix the same by adding to the start of the array instead of pushing collections to the array

see issue https://github.com/adonisjs/adonis-framework/issues/334.

juanyunis found solution by creating a url before the resource like this:

Route.get('customers/names', 'CustomerController.names')
Route.resources('customers', 'CustomerController').except('create', 'edit')

Removed addCollection and addMember in 4.0, since they are confusing and adds very little value. So it's better to keep routes easy to scan with eyes, over doing magical stuff

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

imperez picture imperez  路  4Comments

Extarys picture Extarys  路  4Comments

devcaststudio picture devcaststudio  路  3Comments

douglaszaltron picture douglaszaltron  路  3Comments

danilopolani picture danilopolani  路  4Comments