gmoore
gmoore

Reputation: 5566

Specifying slug in reverse order in Rails routes

We have some namespaced controllers on our app.

It's pretty easy to do this:

/store/pants
/store/shirts/2

In a multi-tenant environment, we would like to do this:

/:tenant_slug/hats
/:tenant_slug/hats/3
/jims-discount-apparel/gloves

And have those routes map to the namespaced controllers:

Store::HatsController
Store::GlovesController

We are not going to use /store/:tenant_slug/hats

I want any route that starts with a :tenant_slug to be mapped to the appropriate controller in the Store namespace.

We have several controllers under Store and would like to avoid listing them all (hats, gloves, shirts, pants, etc...) in routes.rb.

I was trying to use match but I can't quite get it right.

Upvotes: 0

Views: 108

Answers (1)

Benjamin Bouchet
Benjamin Bouchet

Reputation: 13181

Is this not working?

get '/:tenant_slug/hats', to: 'store/hats#index', as: 'hats'

Should generate the following route, and params[:tenant_slug] will contain that part of the URI:

hats GET /:tenant_slug/hats(.:format) store/hats#index

Upvotes: 1

Related Questions