How to add route to root path

In my plugin, I have a route like this in config/routes.rb:

Pfaffmanager::Engine.routes.draw do
  get "/ssh-key/:hostname" => "serverkeys#get_pub_key_by_hostname", constraints: { end

And I can do


and get the public key like I want. :tada:

Not willing to leave good enough alone (and users will almost certainly copy/paste this, so 9 extra characters means nothing), I want to be able to do


I’ve looked at a bunch of other plugins, and it seems like something like this should work:

Discourse::Application.routes.prepend do
  get "/ssh-key/:hostname" => "serverkeys#get_pub_key_by_hostname", constraints: { hostname: /[^\/]+/ }

This doesn’t work at all. A couple things that I have tried (I’ve tried a bunch of things, and really shouldn’t have spent any time on this particular windmill) have looked like they were catching the route correctly, but then I’d need to do . . . something . . . to tell it to look for that controller in pfaffmanager/get_pub_key_by_name.

Did you try following this

and changing the at: parameter accordingly?


Thanks, Rafael! That got me through it.

Basically, I just mounted my plugin at / and then prepended /pfaffmanager to the routes in ....routes.draw. As with most of these issues, the answer is obvious once I find it.


The other way to do it is like this:

1 Like