尝试访问以下 URL 时,我收到 404 错误页面 :
dev.mydomain.com/api
而我的 routes.rb 文件提到这条路线确实存在:
路由文件
constraints :subdomain => 'dev' do
root :to => 'developers/main#index', :as => :developers
namespace 'api', :as => :developers_api do
root :to => 'developers/apidoc/main#index'
end
end
rake 路线
developers /(.:format) {:subdomain=>"dev", :controller=>"developers/main", :action=>"index"}
developers_api_root /api(.:format) {:subdomain=>"dev", :controller=>"api/developers/apidoc/main", :action=>"index"}
Controller
/app/controllers/developers/apidoc/main_controller.rb
class Developers::Apidoc::MainController < Developers::BaseController
def index
end
end
日志
[router]: GET dev.mydomain.com/api dyno=web.1 queue=0 wait=0ms service=14ms status=404 bytes=0
[web.1]: Started GET "/api"
[web.1]: ActionController::RoutingError (uninitialized constant Api::Developers)
最佳答案
我猜问题是你的路线指向 api/developers/apidoc/main
但你的类(class)只有 Developers::Apidoc::MainController
.您不应该使用 api
命名该路由或者将 Api 添加到 Controller 的命名空间 - Api::Developers::Apidoc::MainController
.
关于ruby-on-rails - 带命名空间的 Rails 3 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6788565/