在routes.rb中,我有:
[some routes]
match '/me' => "scores#all_athlete_scores", :constraints => LoggedInChecker
devise_scope :user do
match '/me' => "devise/sessions#new"
end
match '/scores/athlete/:id', :to =>"scores#all_athlete_scores", :as => "all_athlete_scores"
[some more routes]
如果用户已登录,LoggedInChecker 返回 true,如果未登录,则返回 false。如果为 false,第二条路由会选择它并将用户发送到登录页面。
在scores_controller_test.rb中,我有:
def test_get_all_athlete_scores
[set up]
get :all_athlete_scores, :id => @user
end
奇怪的是,scores_controller_test 中的 request.filtered_parameters[:full_path] 是/[email protected] ,而不是/scores/athlete/:id'。
如果我将routes.rb更改为:
[some routes]
match '/scores/athlete/:id', :to =>"scores#all_athlete_scores", :as => "all_athlete_scores"
match '/me' => "scores#all_athlete_scores", :constraints => LoggedInChecker
devise_scope :user do
match '/me' => "devise/sessions#new"
end
[some more routes]
测试工作正常,request.filtered_parameters[:full_path] =/scores/athlete/:id。
对此完全感到困惑 - 当对 all_athlete_scores 进行 GET 时,/me 不应该匹配。
有什么想法吗?
最佳答案
卡里姆,
我认为令人困惑的是,您有一个方法以及一个命名路线“all_athlete_scores”。
get :all_athlete_scores, :id => @user
调用方法 all_athlete_scores 而不是您所期望的命名路线。
可能应该只在此测试中测试该方法的功能,并将路由测试留给另一个测试。
希望有帮助!
关于ruby-on-rails - 一条匹配的路线会覆盖另一条路线,但不应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934787/