我现在的应用程序使用的网址,如:友好的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