如何在 Rails 中获取 link_to 输出 SEO 友好的 url?

发布于 2024-08-30 08:20:43 字数 500 浏览 3 评论 0原文

我的 link_to 标签是:

<%= link_to("My test title",{:controller=>"search", :action=>"for-sale", :id=> listing.id, :title => listing.title, :search_term => search_term}) %>

并产生这个丑陋的 URL:

http://mysite.com/search/for-sale/12345?title=premium+ad+%2B+photo+%5Btest%5D

我怎样才能生成 link_to:

http://mysite.com/search/for-sale/listing-title/search-term/12345

尝试了几种不同的方法,但在网上找不到太多,非常感谢任何帮助!

My link_to tag is:

<%= link_to("My test title",{:controller=>"search", :action=>"for-sale", :id=> listing.id, :title => listing.title, :search_term => search_term}) %>

and produces this ugly URL:

http://mysite.com/search/for-sale/12345?title=premium+ad+%2B+photo+%5Btest%5D

How can I get link_to to generate:

http://mysite.com/search/for-sale/listing-title/search-term/12345

Been trying this a few different ways and cannot find much online, really appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与君绝 2024-09-06 08:20:43

看一下 这个

将其添加到您的 config/routes.rb 中

map.connect ':controller/:action/:title/search_item/:id', :controller=>'search', :action=>'for_sale' 

,重新启动服务器并检查。
希望有帮助:)

Tahe a look at this

add this in your config/routes.rb

map.connect ':controller/:action/:title/search_item/:id', :controller=>'search', :action=>'for_sale' 

restart your server and check.
Hope that helps :)

薆情海 2024-09-06 08:20:43

您需要更改routes.rb 中的URL 结构以匹配您希望URL 的外观,并在控制器方法的args 中相应地解析参数。

You need to change the URL structure in routes.rb to match what you want the URL to look like, and parse the parameters accordingly in your controller method's args.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文