"search", :action=>"for-sale"...">

How to get link_to in Rails to output a friendly SEO URL?

My link_to tag:

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

and creates this ugly URL:

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

How do I get link_to to generate:

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

I tried this in several different ways and can not find much online, I really appreciate any help!

+1
source share
2 answers

Look at this

add this to your config / routes.rb

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

restart the server and check. Hope that helps :)

+1
source

You need to change the structure of the URLs in route.rb to match what you want the URL to look like, and parse the parameters accordingly in the controller arguments.

0
source

Source: https://habr.com/ru/post/1336982/


All Articles