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.
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
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.
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
Related
Please tell me what will be the url helpers for the following code?
scope module: 'admin' do
resources :articles, :comments
end
and
scope '/admin' do
resources :articles, :comments
end
and
namespace :admin do
resources :articles, :comments
end
As per the rails guide document here - Controller namespace and routing
1.
If you want to route /articles (without the prefix /admin) to Admin::ArticlesController, you can specify the module with a scope block
the path will be like
GET articles_path #index action
GET comments_path #index action
If instead, you want to route /admin/articles to ArticlesController (without the Admin:: module prefix), you can specify the path with a scope block:
this will give the following path but the controller will contain Admin:: prefix
GET admin_articles_path # index action
GET admin_comments_path #index action
with namespace, the route will prefix by admin as well as controller needs to have Admin:: module prefix
GET admin_articles_path # index action
GET admin_comments_path #index action
Running the following commands on a console will return the available routes for your application.
rails routes | grep article
rails routes | grep comment
I'm attempting to maintain some legacy code but I'm struggling to figure out why my defined route isn't working.
The error I'm getting is
Started POST "/api/transactions/weight" ...
ActionController::RoutingError (No route matches [POST] "/api/transactions/weight"):
My (simplified) route is as follows
namespace :api do
resources :transactions, only: [:create] do
post "weight", to: "transactions#weight"
end
end
I've also tried moving the route outside the resource :transactions definition like so
namespace :api do
resources :transactions, only: [:create]
post "transactions/weight", to: "transactions#weight"
end
But I'm getting the same error. Am I misunderstanding route definition, or is the problem elsewhere? Thanks
Always check bundle exec rake routes to understand your routes.
The post method defines "/weight" route as a member route (acting on a specific "transactions" resource
# bundle exec rake routes
api_transaction_weight POST /api/transactions/:transaction_id/weight(.:format). api/transactions#weight
By default, if you don't specify on: option, the route inside resources block gets created as member route.
You want to specify it as a collection route [1] via on: :collection
namespace :api do
resources :transactions, only: [:create] do
post "weight", to: "transactions#weight", on: :collection
end
end
# bundle exec rake routes
weight_api_transactions POST /api/transactions/weight(.:format) api/transactions#weight
[1] https://guides.rubyonrails.org/routing.html#adding-collection-routes
The way you defined routes will consider "weight" as the member route. If you want to define it as collection route.
namespace :api do
resources :transactions, only: [:create] do
collection {post :weight}
end
end
The above convention will expose weight as collection route and not member route.If you want to be defined as member post request. Then use following convention
namespace :api do
resources :transactions, only: [:create] do
member {post :weight}
end
end
If you are using api.rb as draw file in routes, then you need to restart your application to reflect changes in routes file.
I'm using Rails 4.2 with Rails Engine to handle my admin panel. I want to move the engine code to the main rails app and part of doing that is moving the routes.
For now, my routes in the Engine my-web-app/lib/admin/config/routes.rb is something like:
Admin::Engine.routes.draw do
resources :countries, only: [:index]
end
And at my app's routes config/routes.rb:
Rails.application.routes.draw do
.. some routes ...
mount GlobalAdmin::Engine => "/admin"
end
Now the engine's route for countries is countries_url NOT admin_countries_url but for URL you'll need to access it with something like admin/countries (this is the default behavior for the engine and I want to keep it this way).
What I did is that I moved my-web-app/lib/admin/config/routes.rb to my-web-app//config/routes.admin.rb and made it look something like:
Rails.application.routes.draw do
resources :countries, only: [:index]
end
And then in my config/application.rb I added something like this:
config.paths["config/routes.rb"] = [
Rails.root.join("config/routes.admin.rb"),
Rails.root.join("config/routes.rb")
]
The problem with this approach is that I have countries_url, however, I can't access it with admin namespace URL like admin/countries. If I add namespace admin something like:
Rails.application.routes.draw do
namespace :admin do
resources :countries, only: [:index]
end
end
Then I'll need to refer to countries url with admin_countries_url which is not the behavior I need.
Any help of how to move the routes from the engine without affecting the previous engine routes?
I'm aware that routes inside an engine are isolated from the application by default. The application and its engines can have routes with the same names, so integrating both can cause some issues, however I want to preserve the exact routes if possible and not to add a namespace and stick with the exact routes names.
Use scope instead of namespace:
Rails.application.routes.draw do
scope path: "/admin" do
resources :countries
end
end
max#MaxBook ~/p/sandbox> rails routes
Prefix Verb URI Pattern Controller#Action
countries GET /admin/countries(.:format) countries#index
POST /admin/countries(.:format) countries#create
new_country GET /admin/countries/new(.:format) countries#new
edit_country GET /admin/countries/:id/edit(.:format) countries#edit
country GET /admin/countries/:id(.:format) countries#show
PATCH /admin/countries/:id(.:format) countries#update
PUT /admin/countries/:id(.:format) countries#update
DELETE /admin/countries/:id(.:format) countries#destroy
I am using the Rails-Api (4) and I want only three routes for my namespaced routes file.
In my routes.rb file, I am trying to do this:
namespace :api do
namespace :v1 do
resources :documents, only: [:get, :create]
resource :system_status, only: [:get]
end
end
rake routes gets me only this:
Prefix Verb URI Pattern Controller#Action
api_v1_documents POST /api/v1/documents(.:format) api/v1/documents#create
If a take the the only: off, it works and gives me all the routes (which I don't want).
I also tried this:
namespace :api do
namespace :v1 do
post '/documents', to: 'documents#create'
get '/documents/:id', to: 'documents#show'
get '/system_status', to: 'system_status#show'
end
end
Gets me this odd output in rake routes:
Prefix Verb URI Pattern Controller#Action
api_v1_documents POST /api/v1/documents(.:format) api/v1/documents#create
api_v1 GET /api/v1/documents/:id(.:format) api/v1/documents#show
api_v1_system_status GET /api/v1/system_status(.:format) api/v1/system_status#show
Not sure what's up with documents#show getting me only api_v1 as it's prefix.
Looks like you're getting mixed up between HTTP verbs and their corresponding Rails actions. There is no resource route for :get, but there are two routes for a GET request, which are :index, and :show
Change your original resource-based routing to this instead:
namespace :api do
namespace :v1 do
resources :documents, only: [:show, :create]
resource :system_status, only: [:show]
end
end
And that should give you the proper routes, plus the correct URL helper prefixes.
I feel like this may be a dumb question, but it's late and my head is melting a bit.. So I appreciate the assistance.
I'm trying to map the url http://localhost:3000/admin to a dashboard controller but i'm epically failing. Maybe this isn't even possible or the completely wrong idea but anyway my routes looks like this and yes
namespace :admin do
resources :dashboard, { :only => [:index], :path => '' }
...
end
and my simple dashboard_controller.rb
class Admin::DashboardController < ApplicationController
before_filter :authenticate_user!
filter_access_to :all
def index
#schools = School.all
end
end
and my view is located in views/admin/dashboard/index.html.erb
thanks for any input
If all you're trying to do is route /admin to that dashboard controller, then you're overcomplicating it by namespacing it like that.
Namespacing with a nested resource like that would mean that it would be /admin/dashboards for the :index action instead of having a clean /admin route (and you can verify that by running rake routes at the command line to get a list of your routes).
Option 1: You meant to namespace it like that
# putting this matched route above the namespace will cause Rails to
# match it first since routes higher up in the routes.rb file are matched first
match :admin, :to => 'admin/dashboards#index'
namespace :admin do
# put the rest of your namespaced resources here
...
end
Option 2: You didn't mean to namespace it like that
Route:
match :admin, :to => 'dashboards#index'
Controller:
# Remove the namespace from the controller
class DashboardController < ApplicationController
...
end
Views should be moved back to:
views/dashboards/index.html.erb
More info: http://guides.rubyonrails.org/routing.html
Regarding to http://guides.rubyonrails.org/routing.html I prefer this
namespace :admin do
root to: "admin/dashboards#index"
resources :dashboard
end
Try this:
namespace :admin do
root to: 'users#index' # whatever. Just don't start with /admin
#resources :dashboards <= REMOVE THIS LINE !
end