2012-03-04 78 views
2

我覺得我有我需要的一切在我的routes.rb,我的控制,我的控制器規範,但由於某種原因,我仍然得到一個路由錯誤(ActionController的:: RoutingError)。這裏是我的控制器:路由錯誤時,運行控制規範(RSpec的)

class HunchController < ActionController::Base 
    protect_from_forgery 

    def results 
    auth_token_key = params[:auth_token_key] 
    user_id = params[:user_id] 
    @user = User.create! 
    @user.auth_token = @user.get_auth_token(auth_token_key, user_id) 
    @recommended_books = @user.get_recommended_books(@user.auth_token) 
    end 
end 

這裏是我的控制器規格:

require 'spec_helper' 

describe HunchController do 
    describe "POST 'results'" do 
    before do 
     @params = { 
     :auth_token_key => "my auth token", 
     :user_id => "my user id" 
     } 
    end 

    it "succeeds" do 
     post :results, @params 
     response.should be_success 
    end 
    end 
end 

而且這裏是我的routes.rb:

MyApplicationName::Application.routes.draw do 
    root :to => 'hunch#index' 

    resources :users 
    post 'hunch/results' => "hunch#results" 
    match '/results' => 'hunch#results' 
end 

編輯:這是我的耙路輸出:

  root  /      {:controller=>"pages", :action=>"index"} 
hunch_results POST /hunch/results(.:format) {:controller=>"hunch", :action=>"results"} 
     results  /results(.:format)  {:controller=>"hunch", :action=>"results"} 
     users GET /users(.:format)   {:action=>"index", :controller=>"users"} 
       POST /users(.:format)   {:action=>"create", :controller=>"users"} 
    new_user GET /users/new(.:format)  {:action=>"new", :controller=>"users"} 
    edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"} 
     user GET /users/:id(.:format)  {:action=>"show", :controller=>"users"} 
       PUT /users/:id(.:format)  {:action=>"update", :controller=>"users"} 

編輯#2:我得到這個錯誤與我的用戶#顯示測試過。下面是實際的錯誤:

1) UsersController#show succeeds 
    Failure/Error: get :show 
    ActionController::RoutingError: 
     No route matches {:controller=>"users", :action=>"show"} 
    # ./spec/controllers/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>' 
+0

請添加您的耙路輸出。 – John 2012-03-04 07:51:39

+0

上面編輯。謝謝。 – jyli7 2012-03-04 19:38:58

+0

什麼是實際路由錯誤? – 2012-03-04 19:42:38

回答

0

好吧,我得到了同樣的問題,當寶石旅程更新到最新版本。而且發現,如果你有這樣的路線:

user GET /users/:id(.:format)  {:action=>"show", :controller=>"users"} 

其中有一個參數(在這種情況下ID),您一定需要呈現在模板的鏈接或控制器使用user_path/user_url變量用PARAM太,所以在你的模板試圖找到:

<%= link_to "linkname", user_path %> 

,並通過更換

<%= link_to "linkname", user_path(@user) %> 

或以任何控制器爲user_path/user_url使用搜索並通過相應user_path(@user)/user_url(@user)替換它們。

+0

非常感謝! – jyli7 2012-07-05 23:46:13