How to get link_to in Rails output an SEO friendly url?

冷暖自知 提交于 2019-12-23 02:36:35

问题


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!


回答1:


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 :)




回答2:


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.



来源:https://stackoverflow.com/questions/2718439/how-to-get-link-to-in-rails-output-an-seo-friendly-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!