Question: How to handle similar urls in rails 4 explicitly

Question

How to handle similar urls in rails 4 explicitly

Answers 1
Added at 2016-12-28 03:12
Tags
Question

i am trying to make more semantic url like below:-

  //with one params
  get 'location/:location' => 'home#show_by_location', as: :show_by_location
  //with two params
  get ':location/:name/'  => 'home#show_details' , as: :show_details

they are almost similar but they are not working as second url never gets called as rails think they are same and hence calls only first one(as expected).So how can i treat both of them differently.

Thanks in advance.

Answers to

How to handle similar urls in rails 4 explicitly

nr: #1 dodano: 2016-12-28 06:12

Swap the order and use a constraint to tell rails that :location cannot be the string 'location':

get ':location/:name/' => 'home#show_details', constraints: { location: /^(location)/ }, as: :show_details
get 'location/:location' => 'home#show_by_location', as: :show_by_location
Source Show
◀ Wstecz