Discourse erroneously requesting plural of route?

In my plugin routes, I had the following (not these exact names, but hopefully the point is clear)

		get  "/u/:username/thing" => "user_thing#index", constraints: { username: RouteFormat.username }
		get  "/u/:username/thing/setup" => "user_thing#setup_index", constraints: { username: RouteFormat.username }
		post "/u/:username/thing/setup" => "user_thing#setup", constraints: { username: RouteFormat.username }

My route map was:

export default function() {
	this.route(
		'user',
		{ path: '/u/:username', resetNamespace: true},
		function() {
			this.route('thing', function() {
				this.route('setup')
			})
		}
	)
}

This has worked so far. Recently, I wanted to add the new routes (note: theyā€™re aligned to root):

		resource :thing_foobar, path: "thing/foobar/:foobar_id"

(Writing these fake names is becoming difficult :confused:)

Now my route map is:

export default function() {
   this.route(
   	'user',
   	{ path: '/u/:username', resetNamespace: true},
   	function() {
   		this.route('thing', function() {
   			this.route('setup')
   		})
   	}
   )

   this.route(
   	'thing',
   	function() {
   		this.route('redirect', { path: '/foobar/:foobar_id' })
   	}
   )
}

Going to mysite.tld/thing/foobar/1, I get redirected to /404

I check my network and I have the following 404:

http://mysite.tld/foobars/1?_=1537817415772

What? Why is it requesting a plural form of my route instead of the actual route? Is it because itā€™s a relative path or something? I donā€™t understand.

1 Like

Iā€™m not sure if this is exactly what caused it, but I was missing a model() { declaration in my route that was giving the plural. I donā€™t know why these are correlated, if they are.

2 Likes

Glad to hear you got it working. I was interested to see what was causing this, so did a little digging:

If you donā€™t define model() explicitly, the built-in Ember implementation is used:

https://github.com/emberjs/ember.js/blob/v2.18.2/packages/ember-routing/lib/system/route.js#L1538-L1565

This ā€˜guessesā€™ the name of the model for the route, and then looks it up using the applicationā€™s ā€˜storeā€™:

https://github.com/emberjs/ember.js/blob/v2.18.2/packages/ember-routing/lib/system/route.js#L1587-L1589

The storeā€™s default behaviour is to construct the URL automatically. It ā€œpluralizesā€ the name of the model, which explains why you saw a request for /foobars

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/adapters/rest.js.es6#L64-L69

6 Likes

So is there anyway to specify a route without a model? Am I missing the point of routes?