[英]Routing Error: uninitialized constant
I'm trying to set up routes for a mobile API, which should have a versioned api-path. 我正在尝试为移动API设置路由,该API应具有版本化的api路径。 I already could make the mobile Auth work, which is implemented in a separate Controller AuthController located in /controllers/api/v1/mobile/ .
我已经可以使移动Auth工作,它是在位于/ controllers / api / v1 / mobile /的单独的Controller AuthController中实现的。
Usage example: 用法示例:
myapp.com/api/v1/mobile/auth
But now I want to register my existing ressources-Controllers to this path-pattern as additional api-routes. 但是现在我想将我现有的ressources-Controllers注册到这个路径模式作为额外的api-routes。 Concrete: this would be the TasksController located at /controllers/tracker/tasks_controller.rb .
具体:这将是位于/controllers/tracker/tasks_controller.rb的TasksController 。 So I added a mobile route to the routes-definition:
所以我在路由定义中添加了移动路由 :
# routes.rb
namespace :tracker, path: 'timetracking' do
resources :tasks, 'jobs'
end
namespace :api do
namespace :v1 do
namespace :mobile do
resources :auth, :only => [:create, :destroy]
namespace :tracker do #added mobile route
resource :tasks, controller: 'tracker/tasks', as: :mobile_tasks
end
end
end
end
But when I call myapp.com/api/v1/mobile/tracker/tasks it results in an error-message: 但是,当我拨打myapp.com/api/v1/mobile/tracker/tasks时 ,会出现错误消息:
Routing Error
uninitialized constant Api::V1::Mobile::Tracker
I especially added the alias :mobile_tasks to this route, to avoid any conflicts with the original tasks-route above. 我特别添加了别名:mobile_tasks到此路由,以避免与上面的原始任务路由发生任何冲突。 Any ideas, how to set the controller properly for this route?
有什么想法,如何为这条路线正确设置控制器?
Update#1 更新#1
Defining this route as a scope instead of a namespace, didn't work aswell. 将此路由定义为范围而不是命名空间,也不起作用。
scope "/api/v1/mobile/tracker" do
resources :tasks, controller: 'tracker/tasks', as: :mobile_tasks
end
But this time, it didn't even resolve the route-path itself. 但这一次,它甚至没有解决路径路径本身。
Routing Error
No route matches [GET] "/api/v1/mobile/tracker/tasks"
I assume it might be a problem, that my additional mobile-api route tries to point to a completely different namespace tracker . 我认为这可能是一个问题,我的附加mobile-api路由试图指向一个完全不同的命名空间跟踪器 。
According to http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing you should use scope instead of namespace. 根据http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing,你应该使用scope而不是namespace。
If you want to route /admin/posts
to PostsController
(without the Admin:: module prefix
), you could use: 如果你想路由
/admin/posts
到PostsController
(没有Admin:: module prefix
),你可以使用:
scope "/admin" do
resources :posts, :comments
end
Late answer, but still might be helpful: 迟到的答案,但仍然可能有帮助:
scope '/v1' do
resources :articles, module: 'v1'
end
controller 调节器
# app/controller/v1/articles_controller.rb
class V1::ArticlesController < ApplicationController
end
Now you should be able to access this url: 现在您应该能够访问此网址:
Adding this answer to get clarity on namespace & scope. 添加此答案以明确命名空间和范围。
When you use namespace, it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace. 使用命名空间时,它将为指定资源的URL路径添加前缀,并尝试在与命名空间相同的方式命名的模块下找到控制器。
# config/routes.rb
namespace :admin do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) admin/posts#index
When we add scope, it will just map the controller action for the given scope patterns. 当我们添加范围时,它将只映射给定范围模式的控制器操作。 No need to define controller under any module.
无需在任何模块下定义控制器。
# config/routes.rb
scope :admin do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) posts#index
Note that, controller is just posts controller without any module namespace. 请注意,控制器只是发布没有任何模块命名空间的控制器。
If we add a path
option to scope it will map to the controller with the path option specified as follows 如果我们向作用域添加
path
选项,它将映射到控制器,并使用如下指定的路径选项
# config/routes.rb
scope module: 'admin', path: 'admin' do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) admin/posts#index
Note that the controller now is under admin module. 请注意,控制器现在位于管理模块下。
Now, if we want to change the name of path method to identify resource, we can add as
option to scope. 现在,如果我们想要更改路径方法的名称以识别资源,我们可以添加
as
范围的选项。
# config/routes.rb
namespace module: 'admin', path: 'admin', as: 'root' do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
root_posts GET /admin/posts(.:format) admin/posts#index
You can see the change in the Prefix Verb. 您可以在前缀动词中看到更改。
Hope it helps others. 希望它能帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.