hephalump
hephalump

Reputation: 6164

Rails 4.2 routing to ignore part of a URL

My first SO question... Please be gentle :)

I have a specific routing requirement for a Rails 4.2 application but I'm struggling to accomplish it. My client offers it's members a website to sell the company's products and earn a commission on sales (kind of like Amway does)... Basically a glorified affiliate program. The pattern of their URL's is https://www.company.com/membername/products/something-you-can-buy. The requirement is that they keep this pattern in the new application I am building for them.

The membername part could be anything, and this application will not have access to the existing user database... We need rails to ignore the membername part of the url and process routing based on the rest of the URL. So if the URL is https://www.company.com/hephalump/products/hamburger we need routing to ignore the hephalump bit, but still keep it in the URL, and process the routing based on the products/hamburger portion. The client also needs the hephalump bit to stay in there throughout the entire application and we need to be able to capture it as a param at the point of purchase (that's easy enough).

I've been bashing my head against the wall with this for two days with no luck... Any help would be greatly appreciated.

Upvotes: 0

Views: 561

Answers (1)

Mori
Mori

Reputation: 27789

There are many ways to do that. Here's one:

 get '/:_member/products/:product_name' => 'products#show'

Substitute the correct controller name prefix for products in products#show and the correct action name for show. In that controller params will have both :_member and :product_name keys, and you can just ignore the :_member key.

You can also add constraints on what pattern can represent a member or a product name, e.g.:

 get '/:_member/products/:product_name' => 'products#show',
   contraint: { _member: /\w+/ }

Upvotes: 1

Related Questions