Route a controller to namespace :admin to /admin - ruby-on-rails

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

Related

how to make dynamic namespace or scopes route in rails

I am trying to make admin route with namespace but it doest trigger to the route
I run rails g controller admin
it created the file app/controllers/admin_controller.rb , app/views/admin/index.html.haml
my namespace look like this
namespace :admin do
controller :admin do
get '/', :index
end
end
it doesn't trigger to localhost:3000/admin it said not found for that route
any idea ??
namespace not only adds a path prefix but it also adds a module nesting to the expected controller. For example:
namespace :blog do
resources :posts
end
Will create the route /blog/posts => Blog::PostsController#index.
In your example Rails expects the controller to be defined as:
# app/controllers/admin/admin_controller.rb
module Admin
class AdminController
# GET /admin
def index
end
end
end
If you just want to add a path prefix or other options to the routes without module nesting use scope instead:
scope :admin, controller: :admin do
get '/', action: :index
end
This will route /admin to AdminController#index
But if this just a one off route you could just write:
get :admin, to: 'admin#index'
Your controller path needs to be in the admin namespace.
So the filename for the admin controller needs to be app/controllers/admin/admin_controller.rb.

No route matches error. Whats wrong with this code?

I'm newbie on rails and getting an error while try to add new method on my controller :(
I have a controller under admin path;
Admin::MyUsersController < ApplicationController
before_filter :......
def index
redirect_to :action => :show_my_action
end
def show_my_action
...
...
end
My controller like this but not this exactly.
In my routes.rb
namespace "admin" do
resources :my_users do
get "show_my_action"
end
end
When my routes.rb is like this, im getting error => No route matches {:action=>"show_my_action", :controller=>"admin/my_users"}
namespace "admin" do
resources :my_users do
get "show_my_action", :on => :collection
end
end
when my routes.rb like this then no error :S
Why im getting this error. I can use first declaration for other controllers which is on root path.
You are adding actions to RESTful actions, if you don't specify a collection, or a member, the route can't know what you want. If you define like this:
namespace "admin" do
resources :my_users do
get "show_my_action"
end
end
How can routes know which route you want:
my_users/show_my_action, or my_users/:id/show_my_action
So, you need to specify it's member or collection:
namespace "admin" do
resources :my_users do
get "show_my_action", :on => :collection
end
end
will have route: my_users/show_my_action, and:
namespace "admin" do
resources :my_users do
get "show_my_action", :on => :member
end
en
will have route: my_users/:id/show_my_action
You can check at Adding More RESTful Actions.
You need to specify whether the action is on a member or a collection. If it's on a member then your URL is admin/my_users/:id/show_my_action. If it's on a collection then it's admin/my_users/show_my_action. Read up on it here: http://edgeguides.rubyonrails.org/routing.html

Map a route to admin/news/show and another to news/show, do I need 2 controllers?

Actually I don't understand how to correctly handle this. I have a situation where news could be managed with admin/edit admin/show admin/news... and similar paths, however I want to give users a page called news/show/1, because actually my news resources are routed under "admin" namespace, how should I handle the fact that I need to bind news routes outside "admin" namespace?
Actually I have only this:
namespace :admin do
resources :news
end
My Idea:
namespace :admin do
resources :news
end
resources :news
Then I'll have:
app/controllers/admin/news_controller.rb
app/controllers/news_controller.rb
Is this correct?
Seeing your answer, I can suggest more simpler routes.
#routes.rb
namespace :admin do
resources :news
end
resources :news, :only => [:show]
If you want index action too, rewrite the last line as:
resources :news, :only => [:index, :show]
You won't need the helper for news_path and news_url. You will get them already built for you.
Ok after working a bit on routes, I understood how to build what I wanted:
namespace :admin do
resources :news
end
get 'news/:id(.:format)' => 'news#show'
This because I don't need all routes for my news, but only show (well I may add index too, but not required at the moment). In this way I can handle everything on 2 different controllers, which is better, because I use somethings like redirects on the news controller which I don't use on Admin::NewsController.
I noticed another important thing, if you build routes in this way news_path and news_url won't be created. Because of this, I had to manually create them in this way in news_helpers:
module NewsHelper
def news_url(record)
url_for controller: 'news', action: 'show', only_path: false, id: record.slug
end
def news_path(record)
url_for controller: 'news', action: 'show', only_path: true, id: record.slug
end
end
(slug is for seo-friendly urls) Then I simply included the helper in my controller in this way:
class NewsController < ApplicationController
include NewsHelper
Everything is worked as I wanted and looks great too.

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

How do you render a rails partial that is outside of a namespace in a view that is inside of a namespace?

I've got a 'static' controller and static views that are pages that don't utilize ruby in their views. For these pages, I have a sitemap partial that is generated programatically and used in the application layout file. Namespaced routes still use the application layout file but are taking the static routes and trying to namespace them too.
Here's the relevant portion of the route file:
namespace :admin do
resources :verse_categories
resources :verses
resources :songs
resources :flowers
resources :visits, :except => [:new, :create]
end
match ':action' => 'static'
root :to => 'static#home'
Here's the error I'm getting:
No route matches {:controller=>"admin/static", :action=>"about"}
Note that about is one of the static pages that the sitemap partial uses.
So, how can I resolve this routing issue so that it's not trying to find my static sites inside of the admin namespace? Any help would be appreciated!
What about:
namespace :admin do
...
get "/about" => "static#about"
end
Or
scope "/admin" do
get "/about" => "static#about"
end
This gist explains how to add directories to the search path for namespaced directories. I ended up doing the following:
class Static::BaseController < ApplicationController
def self._prefixes
super | ["other_directory"]
end
end

Resources