我現在的應用程序使用的網址,如:友好的URL 3
/philsturgeon /philsturgeon/trip_slug
我有連接到用戶在我的模型,像這樣的旅行:
class Trip < ActiveRecord::Base
belongs_to :user
def to_param
"#{user.username}-#{slug}"
end
end
現在我被告知to_param看起來很棒。這意味着我可以使用正常的資源鏈接:
<h4><%= link_to trip.name, trip %></h4>
,而不是手動創建的字符串是這樣的:
redirect_to('/' + current_user.username + '/' + @trip.slug)
問題是,給我一個連字符(或破折號)分隔URL。當我更改URL to_param到#{user.username}/#{slug}
(注意斜線而非短跑),我得到一個錯誤:
ActionController::RoutingError in Home#index
Showing /Users/phil/Scripts/ruby/travlr/app/views/home/index.html.erb where line #27 raised:
No route matches {:action=>"destroy", :controller=>"trips", :id=>#}
Extracted source (around line #27):
24: <%= gravatar trip.user.email, 50 %> 25: 26: 27: <%= link_to trip.name, trip %> 28: 29: 30:
User:
實際上你應該逃避一切:「#{user.username} - #{slug}」。to_s.gsub(/ [] /,' - ') – 2010-11-11 10:33:04
添加以下URL:/ trips/philsturgeon% 2Famerica-2012我需要鏈接到/ philsturgeon/america-2012 – 2010-11-11 16:35:54