Discourse erroneously requesting plural of route?


(Boyned//Kampfkarren) #1

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.


(Boyned//Kampfkarren) #2

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.


(David Taylor) #3

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


(Boyned//Kampfkarren) #4

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