简体   繁体   中英

rails passing multiple params to controller

I have a route for querying "Service" model:

resources :services, :path => 'services'

When GET requests for path /services/sn-uber i take param sn(service name) and find it in database. How to add there another param like sd(service-district)?

For example /services/sn-uber/sd-brooklyn or /services/sd-brooklyn so any param could be omitted.

Add something like this to your routes:

get 'services(/sn/:sn_name)(/sd/:sd_name)', controller: 'services', action: 'show'

Your url will look like:

  • /services/sn/uber/sd/brooklyn
    • params will be sn_name and sd_name
  • /services/sn/uber
    • param will be sn_name
  • /services/sd/brooklyn
    • param will be sd_name

If you want to keep your url like that /services/sn-uber/sd-brooklyn :

get 'services(/:sn_name)(/:sd_name)', controller: 'services', action: 'show'

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