简体   繁体   中英

In rails how do I route domain.com/12345 to /fetch/12345 dynamically?

I'm having a little trouble understand how routes work in Ruby on Rails.

What I'm trying to achieve is have all ID's accessible directly after the domain name, for instance

domain.com/<- ID goes here-> 

routes to

domain.com/fetch/<- entered ID ->

Any push in the right direction would be greatly appreciated.

Thanks a lot

This might be a bad idea; once you put in this general route then any unrecognized url with a single path component is going to end up being handled by your fetch method. Assuming you understand and are okay with that there are several ways that you could do this, the most straightforward:

I assume that you already have /fetch/:id in your routes, something like this to handle /fetch requests in ApplicationController#fetch:

namespace :fetch
  get '/:id' => 'application#fetch'
end

Then you can add a rule at the bottom of your routes like this:

get '/:id' => 'application#fetch'

That should go at the very bottom because you don't want it to override any more specific single-path-component routes.

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