路线不存在?

时间:2014-02-04 19:31:51

标签: ruby-on-rails api routes

我在Rails 4中。我使用的是rails-api gem。我的控制器在controller / api / v1 /中。我目前正在使用的是patients_controller.rb。

我的路线如下:

namespace :api, :constraints => {:subdomain => "api"}, :defaults => {:format => :json} do
  namespace :v1 do
    match "patient/:id", to: "patient#show", via: :get
    match "recording/:id", to: "recording#delete", via: :delete
  end
end

当我运行rake路线时,我得到:

api_v1 GET    /api/v1/patient/:id(.:format)   api/v1/patient#show {:format=>:json, :subdomain=>"api"}
DELETE /api/v1/recording/:id(.:format) api/v1/recording#delete {:format=>:json, :subdomain=>"api"}

但是当我从本地主机/api/v1/patient/2.json处获得时,我从服务器收到以下错误:

Started GET "/api/v1/patient/2.json" 
ActionController::RoutingError (No route matches [GET] "/api/v1/patient/2.json")

我重新启动了服务器。我重新启动了终端。我错过了什么?

1 个答案:

答案 0 :(得分:0)

将本地添加到您的子域

namespace :api, :constraints => {:subdomain => /^(?!www|api|local)(\w+)/}, :defaults => {:format => :json} do
  namespace :v1 do
    match "patient/:id", to: "patient#show", via: :get
    match "recording/:id", to: "recording#delete", via: :delete
  end
end