2016-03-06 86 views
0

我是新來的鐵軌,我試圖建立API下面的代碼學校教程。 我試圖發佈到'/ users'路徑時出現此錯誤。沒有路線匹配[郵政]「/用戶」

路線文件代碼:

Rails.application.routes.draw do 
    namespace :api,constraints: {subdomain: 'api'}, path: '/' do 
    resources :users, except: :delete 
    end 
end 

和測試代碼:

require 'test_helper' 
class CreatingUsersTest < ActionDispatch::IntegrationTest 
    test 'create users' do 
    post api_users_path, 
     {user: {name: 'test', email:'[email protected]'}}.to_json, 
     {'Accept' => Mime::JSON, 'Content-Type': Mime::JSON.to_s} 
    assert_equal response.status, 201 
    assert_equal response.content_type, Mime::JSON 
    user = json(response.body) 
    assert_equal api_user_url(user[:id]), response.location 
end 
end 

當我用耙路線:

api_users GET /users(.:format) api/users#index {:subdomain=>"api"} 
       POST /users(.:format) api/users#create {:subdomain=>"api"} 
    .... 

回答

0

在路線,你約束的子域be api

namespace :api,constraints: {subdomain: 'api'}, path: '/' do 

但隨後在測試你叫api_user_path

post api_users_path 

使用一個通用的主機名(test.host),而不是api主機名。解決方案是將特定的主機傳遞給滿足要求的助手。

post api_users_path(host: "api.test.host")