简体   繁体   中英

Ruby on Rails Custom link_to

I have a list posts, which when clicked on should show the url as posts/this+is+the+title . After doing this, how would I catch this in the controller to fetch data from the database?

     <% @posts.each do |fq| %>
     <%= link_to fq.title,fq%>
     <p><%= fq.content %></p>
     <hr />
     <% end %>

Assuming a title is "Ruby on Rails" with id =3 The above code, generates a url like this 'localhost:3000/posts/3'. But I would want the link to be 'localhost:3000/posts/ruby+on+rails'

You are looking for pretty urls those includes some text instead of ids as rails generates. Then you must look into friendly_id gem . There is good screen cast you would be interested in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM