简体   繁体   English

在Rails中,如何动态将domain.com/12345路由到/ fetch / 12345?

[英]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.

相关问题 Rails与组织/用户相同的顶级URL路由:domain.com/user和domain.com/organization的工作原理相同 - Rails same top-level URL route for organizations/user: domain.com/user and domain.com/organization works the same 如何配置nginx代理到rails应用程序?所以我不必说domain.com:port - How to config nginx to proxy to rails app? so that i dont have to say domain.com:port Rails:生产中缺少资产(没有路线匹配[GET]“ /assets/application-12345.css”) - Rails: assets are missing in production(No route matches [GET] “/assets/application-12345.css”) 如何在Apache上的Ruby on Rails中将domain.com更改为staging.domain.com - How to change domain.com to staging.domain.com in Ruby on Rails on Apache 如何在Rails中按域/子域进行路由 - How do I route by domain / subdomain in rails Rails:当输入为domain.com/?r=123时,在路由中重定向 - Rails: Redirect in routes when input is domain.com/?r=123 Rails / Passenger / Nginx:将www.domain.com重定向到domain.com(以防止SSL警告) - Rails/Passenger/Nginx: Redirecting www.domain.com to domain.com (to prevent SSL warning) Rails / Passenger / Apache2:从www.domain.com重定向到domain.com - Rails/Passenger/Apache2: Redirect from www.domain.com to domain.com 在Rails中将http://domain.com/subpage重定向到http://www.domain.com/subpage - Redirect http://domain.com/subpage to http://www.domain.com/subpage in Rails 在Ruby on Rails中,有一种方法可以说,尝试使用Product.find(12345),但是如果找不到,就不会出错? - In Ruby on Rails, is there a way to say, try Product.find(12345) but don't error if not found?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM