简体   繁体   中英

Dynamic routing rails

I'm working in an app with the following route definition.

get 'list/:city(/:zone)(/:district)'

It works perfectly when I want to catch URL like

/list/milan
/list/milan/milan-ovest
/list/milan/milan-ovest/brera

Now, we are moving to some pretty URL where at the end we need to place a pretty name. Something like this

 /list/milan/renting-apartment-in-milan
 /list/milan/milan-ovest/renting-apartment-in-milan-center
 /list/milan/milan-ovest/brera/renting-apartment-in-brera

I know about wild card, but I wasn't able to find a working solution. Actually the additional element could be ignored, meaning it doesn't offer any useful information.

Thanks everyone.

You can always stub in a glob-style path:

get 'list/*path'

That will capture an arbitrary number of elements after that point and provide them to you as params[:path] which will be an array.

The down-side of this is they won't be broken out in advance, but you can always take care of that through assignment:

city, zone, district, *extra = params[:path]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM