我是rails的新手,我正在尝试按照Code School教程构建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:'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
当我使用rake路线时:
api_users GET /users(.:format) api/users#index {:subdomain=>"api"}
POST /users(.:format) api/users#create {:subdomain=>"api"}
....
答案 0 :(得分:0)
在路线中,您将子域约束为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")