HenryTK
HenryTK

Reputation: 1287

Dynamic routes in rails

I have a rails application which, among other things, provides a simple wrapper around API calls to a third-party service. I want to set up a route which starts with /api, but anything added on to the end of it is taken as a string variable. For example, if a client requests:

/api/apps/guid/details

...then I want to invoke the index action of the controller ApiController and make the string /apps/guid/details available to it.

I have read through the documentation on controllers and routes, but everything seems to assume that /apps/guid/details will be resources within my app, when actually I don't care about the structure of anything after /api.

How can I set up a route which allows me to do this?

Upvotes: 2

Views: 6147

Answers (1)

eiko
eiko

Reputation: 5335

You can use globbing in your config/routes.rb:

get "/api/*path", to: "api#index"

Which would be accessible in the controller via params[:path]

Details can be found in the rails guide.

Upvotes: 6

Related Questions