Michael Hartl教程,没有路线匹配

时间:2014-03-18 23:20:12

标签: ruby-on-rails rspec routes railstutorial.org

我收到此错误:

  

故障:

     

1)UsersController DELETE'destroy'应该签出用户

Failure/Error: delete :destroy
ActionController::UrlGenerationError:
  No route matches {:controller=>"users", :action=>"destroy"}

我的测试是:

it "should sign a user out" do 
  test_sign_in(Factory(:user))
  delete :destroy
  expect(controller).to_not be_signed_in
  expect(response).to redirect_to(root_path)
end

test_sign_in函数位于规范助手中:

  

def test_sign_in(user)

 controller.sign_in(user)
     

我的佣金路线:

      Prefix Verb   URI Pattern               Controller#Action
sessions_new GET    /sessions/new(.:format)   sessions#new
   users GET    /users(.:format)          users#index
         POST   /users(.:format)          users#create
new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
    user GET    /users/:id(.:format)      users#show
         PATCH  /users/:id(.:format)      users#update
         PUT    /users/:id(.:format)      users#update
         DELETE /users/:id(.:format)      users#destroy
sessions POST   /sessions(.:format)       sessions#create
new_session GET    /sessions/new(.:format)   sessions#new
 session DELETE /sessions/:id(.:format)   sessions#destroy
    root GET    /                         pages#home
 contact GET    /contact(.:format)        pages#contact
   about GET    /about(.:format)          pages#about
    help GET    /help(.:format)           pages#help
  signup GET    /signup(.:format)         users#new
  signin GET    /signin(.:format)         sessions#new
 signout GET    /signout(.:format)        sessions#destroy
pages_home GET    /pages/home(.:format)     pages#home

任何人都知道如何解决此错误?

2 个答案:

答案 0 :(得分:1)

您的路线定义为

DELETE /users/:id(.:format)      users#destroy

这意味着路线期待像

这样的东西
DELETE /users/4

查看您的测试,您只是请求DELETE /users,这是源于此错误消息:

ActionController::UrlGenerationError: No route matches {:controller=>"users", :action=>"destroy"})

因此,您需要修改测试以处理路径的:id部分。这是未经测试的,但您大致正在寻找:

user = Factory(:user)
test_sign_in(user)
delete :destroy, id: user.id

答案 1 :(得分:0)

哦,上帝!!我发现了错误。我不小心在users_controller_spec中编写代码,我应该在sessions_controller_spec中执行此操作。 感谢Andreas试图提供帮助!