Reputation: 431
I'm running into a couple of errors when i try to define a root to a namespace. To recreate this, I'll rebuild a project from scratch.
rails new rails_test
cd rails_test
rails generate controller admin
rake db:migrate
Now I put some boilerplate in app/controllers/admin_controller.rb
class AdminController < ApplicationController
def index
end
end
in app/views/admin/index.html.erb
<p>Index</p>
and finally in config/routes.rb
root to: 'admin#index'
This all works perfectly, when i start the server and hit '/' (root) url (it goes to the admin index page), but it's not what I want.
This is meant to be part of a bigger project and I want to hit the /admin url and get the admin index so (following from http://guides.rubyonrails.org/routing.html#using-root) I change routes.rb to:
namespace :admin do
root to: "admin#index"
end
but I get a routing error:
uninitialized constant Admin
with a routes list containing 1 line:
admin_root_path GET /admin(.:format) admin/admin#index
My thinking from reading the end of this last line is that I'm already in the admin namespace so maybe i don't need to specify the controller index is in so I try changing routes to:
namespace :admin do
root to: "index"
end
But that gives me an ArgumentError saying "missing :action" on the 'root to: "index"' line.
I can get around it by using scope, but it looks like using namespace is a bit cleaner and I want to understand whats going wrong here.
Ruby/Rails versions
ruby -v -> ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
rails -v -> Rails 4.0.0
Any help is apprechiated
Upvotes: 1
Views: 2388
Reputation: 2270
I think what you really want is something like
namespace :admin do
get 'other'
end
get 'admin' => 'admin#index'
This allows for the index
and other
methods to be in straightforward AdminController
controller in usual directory, views to go in views/admin
etc.
You could probably also use
resource :admin, only: [:index] do
get 'other'
end
Upvotes: 0
Reputation: 500
on rails 4 in routes two roots not permited now
use get "/admin" => "admin/admin#index", :as => "admin"
Type in terminal
rails generate scaffold Admin::User username email
rake db:migrate
if only Controller type this
rails generate controller Admin::User
Upvotes: 0
Reputation: 2656
namespace
namespaces controllers as well. Change yours to
class Admin::AdminController < ApplicationController
def index
end
end
And move it under app/controllers/admin
. That's what admin/admin#index
means. :)
Upvotes: 1
Reputation: 10111
According to rubyonrails.org guide on routing you should be able to do something like this. it should looks like this
namespace :admin do
root to: "admin#index"
end
root to: "home#index"
What do you get with rake routes?
Upvotes: 0