If I rename the top-level controller then my module won’t be inside it
any more, which I thought was the point of modules.
By ‘top-level controller’ do you mean
app/controllers/admin_controller.rb? This controller is not in a module.
The only time a controller is in a module is when it is in a
sub-directory of the base controller directory
(app/controllers//some_controller.rb). Conversely, any
controller NOT in a sub-directory is NOT in a module. I hope I am not
confusing it more.
As I understand it, when an incoming request has a controller named
“admin/things”, Rails looks for a controller called “things_controller”
in the directory app/controllers/admin.
By default no, this is not what Rails does. When an incoming request has
a controller named “admin/things” how does rails know which part of the
URL is referencing the ‘controller’ and which part is referencing the
‘action’?
Short answer.
It looks at config/routes.rb.
Long answer.
Remember we are talking about default Rails behavior here, not a
modified route as was suggested in my previous post. Ok, so the default
route that Rails uses is:
map.connect ‘/:controller/:action/:id’
This in basically says, for each incoming request, I understand the URL
to be implying the following:
o The string (if there is one) in the spot labeled ‘controller’ is what
I should use as the controller.
o The string (if there is one) in the spot labeled ‘action’ is what I
should use as the action.
o The string (if there is one) in the spot labeled ‘id’ is what I should
use as the id.
That means, if you pass it a URL that is in the form of ‘admin/things’
it will do the following:
o Assume the controller is ‘admin’ since the string in the controller
spot
is ‘admin’.
o Assume the action is ‘things’ since the string in the action spot is
‘things’.
o Assume there is no id since one is not specified.
At this point, Rails will try to find a controller named
admin_controller.rb in app/controllers/admin_controller.rb and execute
the action (method) ‘things’. However, in your setup, it finds the
controller it is looking for but there is no method named ‘things’. This
is why you get ‘Unknown action No action responded to things’.
Doesn’t this also mean that I want to have a top-level controller
called “admin”?
No.
In fact, you probably don’t even need
app/controllers/admin_controller.rb if I understand what you are trying
to do.
If I were you, I would get the ‘Agile Web D. with Rails’. There
are entire chapters that explain this stuff. It really helped me to
understand it more clearly.
http://www.pragmaticprogrammer.com/titles/rails/index.html