簡體   English   中英

沒有路由匹配[POST]“ / users”

[英]No route matches [POST] “/users”

我是Rails的新手,我正在嘗試遵循Code School教程來構建API。 我在嘗試發布到“ /用戶”路徑時收到此錯誤。

路由文件代碼:

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:'test@test.com'}}.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"}
    ....

在路由中,您將子域限制為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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM