0

我有一个 Rails 路线设置如下:

match ':controller/:id/:action'
# match 'teams/:id' => "teams#show" # doesn't have any additional effect, which makes sense to me
resources :teams, :only => [:index, :show]

这样我可以说它会/teams/cleveland-indians调用teams#show. :id => 'cleveland-indians'效果很好。我的问题是这url_for并不能完全满足我的要求。在我的视图/团队/索引视图中,我得到了这种行为:

url_for(:id => "cleveland-indians") # => /teams/cleveland-indians/index
url_for(:id => "cleveland-indians", :action => :show) # => /teams/cleveland-indians/show

当然,第二个行为我想要的方式,但我想/show最后摆脱不必要的。我不太了解这些助手是如何工作的,但我猜它会知道这show是具有指定 id 的 GET 的默认操作,与路由引擎相同。无论如何,对我来说最好的方法是什么?还是我只是做错了?

4

1 回答 1

1

'resources' line should already provide you with the routes you probably want so you can just remove first 'match' line.

Note that you can also use 'teams_path', 'team_path("cleveland-indians")' instead of 'url_for'.

于 2011-03-15T14:14:35.730 回答