我不确定如何使我的网址更整洁或更漂亮。能不能给我一个建议
我目前有这个网址:
http://www.example.com/speed-meetings/dine%20with%20index%20venture%20capital
并希望它看起来像下面的网址:
http://www.example.com/speed-meetings/dine-with-index-venture-capital
我的路线档案:
get 'speed-meetings/:title', controller: 'speed_meetings', action: 'show'
视图/活动/ index.html.erb
<td><%= link_to 'show', "/speed-meetings/#{event.title}" %></td>
答案 0 :(得分:3)
您可以使用parameterize
方法让它更漂亮。
<%= link_to 'show', "/speed-meetings/#{event.title.parameterize}" %>
你可以看看友善的身份&#39;宝石。使用slugs是件好事。
https://gist.github.com/cdmwebs/1209732 - 这是关于友好网址的精彩文章。
答案 1 :(得分:1)
使用stringex gem:
# Gemfile
gem 'stringex'
# A simple prelude
"simple English".to_url => "simple-english"
"it's nothing at all".to_url => "its-nothing-at-all"
"rock & roll".to_url => "rock-and-roll"
# parameterize will not fare well with these cases:
"$12 worth of Ruby power".parameterize => "12-worth-of-ruby-power" # oops
# Let's show off
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power"
"10% off if you act now".to_url => "10-percent-off-if-you-act-now"
# You dont EVEN wanna rely on Iconv for this next part
"kick it en Français".parameterize => "kick-it-en-franais" # oops
"kick it en Français".to_url => "kick-it-en-francais"
"rock it Español style".to_url => "rock-it-espanol-style"
"tell your readers 你好".to_url => "tell-your-readers-ni-hao"
class Thing < ApplicationRecord
acts_as_url :name
def to_param
url
end
end