Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing Error: uninitialized constant

I'm trying to set up routes for a mobile API, which should have a versioned api-path. I already could make the mobile Auth work, which is implemented in a separate Controller AuthController located in /controllers/api/v1/mobile/.

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. Concrete: this would be the TasksController located at /controllers/tracker/tasks_controller.rb. 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:

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. Any ideas, how to set the controller properly for this route?

Update#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.

like image 865
loybert Avatar asked Apr 23 '12 17:04

loybert


3 Answers

According to http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing you should use scope instead of namespace.

If you want to route /admin/posts to PostsController (without the Admin:: module prefix), you could use:

scope "/admin" do
  resources :posts, :comments
end
like image 106
Krists Avatar answered Nov 08 '22 17:11

Krists


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.

# 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

# 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.

# 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.

like image 32
Ashik Salman Avatar answered Nov 08 '22 16:11

Ashik Salman


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:

http://localhost:3000/v1/articles

like image 1
tokhi Avatar answered Nov 08 '22 17:11

tokhi