[英]Rails routes formatted params in URL
I have the following in my routes: 我的路线中有以下几点:
match '/car-rentals(/:address)' => 'search#search', via: :get
I can visit: 我可以访问:
localhost:3000/car-rentals/montreal 本地主机:3000 / car-rentals /蒙特利尔
Shows all available cars for Montreal. 显示蒙特利尔所有可用的汽车。
When I enter: localhost:3000/car-rentals/Montreal Quebec
当我输入时:
localhost:3000/car-rentals/Montreal Quebec
My URL becomes localhost:3000/car-rentals/Montreal%20quebec
我的网址变为
localhost:3000/car-rentals/Montreal%20quebec
I would like it to be: localhost:3000/car-rentals/montreal-quebec
我希望它是:
localhost:3000/car-rentals/montreal-quebec
I tried using to_param
in my model. 我尝试在模型中使用
to_param
。
class Search < ActiveRecord::Base
def to_param
"#{address.parameterize}"
end
end
Any other ways to achieve this? 还有其他方法可以做到这一点吗?
Also worth mentioning. 还值得一提。 When I stick
binding.pry
in to_param
and reload the page, it doesn't stop the process, so not getting there. 当我坚持
binding.pry
在to_param
并重新加载页面,它不会停止的过程中,所以不会到达那里。
def to_param
binding.pry # on page load, not getting here
"#{address.parameterize}"
end
Thanks in advance! 提前致谢!
There are several gems that generate slugs for SEO friendly urls. 有一些宝石会为SEO友好的URL生成标签。
I've used https://github.com/norman/friendly_id in the past and it worked fine. 我过去曾经使用https://github.com/norman/friendly_id ,而且效果很好。 It also has very helpful documentation.
它还具有非常有用的文档。
There is also this: https://github.com/Sutto/slugged 还有这个: https : //github.com/Sutto/slugged
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.