[英]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. 我在理解Ruby on Rails中的路由工作方式时遇到了一些麻烦。
What I'm trying to achieve is have all ID's accessible directly after the domain name, for instance 我想要实现的是例如在域名之后直接访问所有ID
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.
一旦您采用了这种通用方法,那么任何带有单个路径组件的无法识别的url最终都将由您的fetch方法处理。 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: 我假设您的路由中已经有/ fetch /:id,类似这样的内容可以处理ApplicationController#fetch中的/ 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. 这应该放在最底端,因为您不希望它覆盖任何更特定的单路径组件路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.