0

I'm building an admin control panel (attempting to ;) ).

I have been looking at Backend administration in Ruby on Rails and as suggested I am trying to make Admin::AdminController that checks for admin and sets the layout etc.

But I'm also trying to set a route in it that routes /admin to /admin/dash

From my understanding of reading http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing , specifically section 2.6,

Admin::AdminController

tells rails that Admin is the name space, AdminController is the controller which is a subclass (extension?, implementation of the interface?) of ApplicationController. Which would imply the controller should live in app/controllers/admin/ and be called admin_controller.rb.

But what I want is

AdminController

I get errors like:

uninitialized constant Admin::Controller

My code for the route:

  match :admin, :to => 'admin/admin#dash'
  namespace :admin do
    # Directs to /admin/resources/*
    match '/dash', to: '#dash'
    resources :users, :pictures
  end

I have put the controller in app/controllers/admin, app/controllers and the combinations with

class Admin::AdminController < ApplicationController
  before_filter :admin_user
  # / ** STATIC ADMIN PAGES ** /
  def dash
  end
end

or class AdminController < ApplicationController.

Edit: Maybe it's my understanding of routing. Example:

namespace :admin do
  get "/dash"

vs.

namespace :admin do
  match "/dash" to "admin#dash"

vs.

namespace...
  match "/dash" to "#dash"

The first one makes it so i can display a dash via the controller, i.e. admin/dash would be controlled by

AdminController < ApplicationControler
  def dash
  end

Does the second one route admin/admin/dash to admin/dash?

TL/DR: I think my confusion comes from syntax or my poor understanding of RESTful practices or maybe even class / object inheritance in ruby.

Thanks for helping this n00b out. :)

Side question: can I change my code to be minimized until someone opens it like a spoiler so it doesn't crowd things up if I find more information and add it?

Community
  • 1
  • 1
Fatlad
  • 324
  • 5
  • 21

2 Answers2

3

I think your initial approach was correct, but you need to change it a little.

1) insert the /admin => /admin/dash inside the namespace (imho, its better to redirect it)

match 'admin' => redirect('admin/dash')

or

namespace :admin do
  match '/', to: 'admin#dash'
end

2) You can't match '/dash' to '#dash' since you're not inside a resource block, you're inside a namespace block, so it doesnt' have implied controller.

namespace :admin do
  match 'dash', to: 'admin#dash'
  # This will go to Admin::AdminController#dash
  # (first Admin because of the namespace,
  #  and the second because of the controller name)
end

hope it works :D

yoavmatchulsky
  • 2,962
  • 1
  • 17
  • 22
  • Awesome, this is a great explanation of what each name does! Why do you prefer redirect? – Fatlad Jul 05 '12 at 07:06
  • I don't like two URLs to have the same functionality, but its probably because I come from the world of SEO. Both solutions (redirect or not) are equally good. – yoavmatchulsky Jul 05 '12 at 07:15
  • Would it be better if I just picked one? – Fatlad Jul 05 '12 at 17:04
  • 1
    you need to pick one :) if you use the **redirect()**, when you go to `example.com/admin`, it will redirect to `example.com/admin/dash`, if you use **match '/'**, `example.com/admin` will display the same content as `example.com/admin/dash` – yoavmatchulsky Jul 05 '12 at 17:08
  • Ahh ok thanks! I ended up just redirecting to /admin, thanks for the help :) – Fatlad Jul 06 '12 at 05:35
0

What you want is "scope" in routing.

scope "/admin" do
  resources :articles
end

Which will route /admin/articles to ArticlesController (without Admin:: prefix)

Documentation covers almost every possible case. http://edgeguides.rubyonrails.org/routing.html

tundrax
  • 21
  • 1