如何在 Rails 3 中生成带有尾部斜杠的链接?
我正在将现有网站从 PHP 移植到 Ruby on Rails 3,并且必须保持 url 不变。
我有路线:
get 'companies/' => 'companies#index', :as => :companies
在我有的视图文件中:
link_to 'Companies', companies_path
这会生成 url“http://website.com/companies”而不是“http://website.com/companies/”。
我想要在网址末尾添加斜线。是否可以?
I'm porting the existing website from PHP to Ruby on Rails 3 and I have to keep the urls unchanged.
I have the route:
get 'companies/' => 'companies#index', :as => :companies
In a view file I have:
link_to 'Companies', companies_path
and this generates the url "http://website.com/companies" instead of "http://website.com/companies/".
I want the slash at the end of the url. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将其添加到您的 application.rb 中:
这样所有路由都会自动生成,并带有尾部斜杠,无需修改项目中的每个链接。
You can add this to your application.rb:
This way all routes will be generated with a trailing slash automatically, with no need to modify each link in your project.
只需执行以下操作:
此处的文档。
Simply do as follows:
Documentation here.
我找不到任何参考,但将
trainling_slash: true
添加到路线定义中也可以(并避免重复)。这是用 Rails 3.2.13 测试的:
I couldn't find any references, but adding
trainling_slash: true
to the route definition also works (and avoids repeating oneself).This was tested with Rails 3.2.13:
对于 Rails 3.2:
For rails 3.2: