Discourse erroneously requesting plural of route?

AI-generated summary

A user, Kampfkarren, encountered an issue where Discourse was incorrectly requesting a pluralized route (e.g., /foobars/1) instead of the expected /thing/foobar/1. This occurred after adding a new route in their Ember application. The problem was traced to the absence of a model() method in the route definition. david explained that without an explicit model() method, Ember’s default behavior attempts to infer the model name, which it then pluralizes when constructing the URL for the store. This led to the incorrect request for /foobars/1. Kampfkarren then questioned whether it’s possible to define a route without a model.

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.

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.

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:

This ‘guesses’ the name of the model for the route, and then looks it up using the application’s ‘store’:

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

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