Creating a route in a plugin


(Arran Scott) #1

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?


(Arran Scott) #2

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).


(Robin Ward) #3

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.


(Arran Scott) #4

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

Thanks!