[英]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.