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

  curl https://www.pfaffmanager.com/pfaffmanager/ssh-key/test.myforum.us

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

  curl https://www.pfaffmanager.com/ssh-key/test.myforum.us

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: /[^\/]+/ }
end

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

https://github.com/discourse/discourse/blob/master/plugins/discourse-narrative-bot/plugin.rb#L125-L131

and changing the at: parameter accordingly?

2 Likes

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.

2 Likes

The other way to do it is like this:

1 Like