如何让 Rails 不忽略路由中的尾部斜杠? [英] How to make Rails do not ignore trailing slashes in the routes?

查看:48
本文介绍了如何让 Rails 不忽略路由中的尾部斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在 routes.rb 中有死记硬背:

For example, I have the rote in routes.rb:

get 'companies/' => 'companies#index', :as => :companies

有一种用斜杠生成链接的方法(如http://website.com/companies/"):

There is the way to generate link with the trailing slash (like "http://website.com/companies/"):

link_to 'Compaines', companies_path(:trailing_slash => true)

但我希望 Rails 生成带有尾部斜杠的链接,如果它默认存在于路由中:

But I want Rails to generate link with trailing slash if it is present in the route by default:

link_to 'Companies', companies_path

现在它会生成类似http://website.com/companies"的内容.如何解决?

Now it generates something like "http://website.com/companies". How to fix it?

推荐答案

我不知道为什么你需要你的路由来理解尾随斜杠.是否将尾部斜杠放在 Rails 上并不重要.它应该以任何一种方式响应.

I'm not sure why you need your routes to understand that there is a trailing slash. It doesn't matter if you put the trailing slash to Rails. It should respond either way.

如果您希望所有链接都带有斜杠,可以在 application.rb 中添加以下内容:

If you want all of your links to have a trailing slash, you can put the following in your application.rb:

config.action_controller.default_url_options = { :trailing_slash => true }

这将使 Rails 生成的所有链接都有一个尾部斜杠.现在,如果您试图拒绝任何不包含尾部斜杠的路由,那么我不知道该怎么做.

This will make all links generated by Rails have a trailing slash. Now, if you're trying to reject any route that does not contain a trailing slash, then I'm not sure about how to do that.

这篇关于如何让 Rails 不忽略路由中的尾部斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆