Ruby on rails Api rails:没有路由匹配

Ruby on rails Api rails:没有路由匹配,ruby-on-rails,api,curl,Ruby On Rails,Api,Curl,我在教程中使用了Rails5API,现在出现了一个路由错误 Curl命令Curl-H“授权:令牌令牌=8NjQH4YVWQdUIve4xDQBaArr”http://localhost:3000/v1/users显示404 not found(未找到)消息,并且在我的术语中,在服务器运行的地方会出现“no route matches”(无路由匹配) 没有与[GET]“/v1/users”匹配的路由 所以我检查了铁路线路。输出为: v1_users GET /v1/users(.:forma

我在教程中使用了Rails5API,现在出现了一个路由错误

Curl命令
Curl-H“授权:令牌令牌=8NjQH4YVWQdUIve4xDQBaArr”http://localhost:3000/v1/users
显示404 not found(未找到)消息,并且在我的术语中,在服务器运行的地方会出现“no route matches”(无路由匹配)

没有与[GET]“/v1/users”匹配的路由

所以我检查了
铁路线路
。输出为:

v1_users GET    /v1/users(.:format)     api/v1/users#index {:subdomain=>"api"}
         POST   /v1/users(.:format)     api/v1/users#create {:subdomain=>"api"}
 v1_user GET    /v1/users/:id(.:format) api/v1/users#show {:subdomain=>"api"}
         PATCH  /v1/users/:id(.:format) api/v1/users#update {:subdomain=>"api"}
         PUT    /v1/users/:id(.:format) api/v1/users#update {:subdomain=>"api"}
         DELETE /v1/users/:id(.:format) api/v1/users#destroy {:subdomain=>"api"}
我们注意到我们有GET/v1/users和/:id

如何解决这个问题

routes.rb:

Rails.application.routes.draw do
  constraints subdomain: 'api' do
    scope module: 'api' do
      namespace :v1 do
        resources :users
      end
    end
  end
end
用户\u controller.rb和api\u controller.rb在/app/controller/api/v1/


如果您需要代码的另一部分(控制器、模型……),请毫不犹豫地询问

问题在于您的路由具有子域约束。因此,您的api只能在
http://api.example.com/v1/users

如果不通过apache或nginx设置虚拟主机、编辑主机文件或使用类似的服务,则无法将子域与localhost一起使用

也可以将其转换为路径约束:

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :users
    end
  end
end

问题是路由具有子域约束。因此,您的api只能在
http://api.example.com/v1/users

如果不通过apache或nginx设置虚拟主机、编辑主机文件或使用类似的服务,则无法将子域与localhost一起使用

也可以将其转换为路径约束:

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :users
    end
  end
end

谢谢!我只是让'scope module:'api'do',我没有添加'namespace'api'do'来使用curl和url是的,这只是你想要
http://example.com/api/v1/users
。如果你设置了一个隧道,这样你就可以通过子域访问你的rails服务器,你就不需要改变你的路由了。非常感谢!我只是让'scope module:'api'do',我没有添加'namespace'api'do'来使用curl和url是的,这只是你想要
http://example.com/api/v1/users
。如果您设置了一个隧道,以便可以通过子域访问rails服务器,则无需更改路由。