Creating a route in a plugin

AI-generated summary

This discussion is about creating a route in a Discourse plugin. arrancscott initially had trouble setting up a route /users/:username/route in their plugin, but eventually figured it out by defining the route mapping differently. eviltrout provided a link to a tutorial on creating admin routes, noting it’s preferable to create plugin routes in the admin section when possible. arrancscott mentioned following that tutorial but couldn’t achieve their specific goal from it.

Hey guys,

I need a bit of help from someone familiar with creating routes in plugins.

I’ve created a plugin and have a route specified in my plugin.rb file like so:

register_asset 'javascripts/discourse/templates/user/route.hbs'

after_initialize do
 Discourse::Application.routes.append do
   get 'users/:username/route' => 'users#show', constraints: {username: USERNAME_ROUTE_FORMAT}
 end
end

I also have a ‘*-route-map.js.es6’ file which declares this route like so:

export default function() {
   this.resource('user', function() {
   this.route('route', {path: 'route'});
 });
}`

I literally want to create a route like ‘/users/:username/route’ but none of what I’m doing seems to be working and I’m getting the default 404 page when I go to that URL.

I’ve looked at how a few plugins do it such as the tagging one and I seem to be doing everything correctly so I can’t figure out where I’m going wrong. Can someone help please?

Fixed, after a few hours of head scratching.

export default {
 resource: 'user',
 path: 'users/:username',
 map() {
  this.route('route', { path: '/route' })
 }
}

Posted this incase another user stumbles across this post looking for an answer (which is exactly what I did).

There is also a tutorial here which shows how to create admin routes. It is preferable to create plugin routes in the admin section whenever possible.

Yeah, I followed that but couldn’t figure out what I wanted to do from that tutorial.

Thanks!