controllers/views in sub-folders and broken routes

I'm having some issues with routing when using controllers in sub-folders.

I have controllers in an admin folder that look like this:

class Admin::UsersController < ApplicationController end

My link_to_remote calls look like this:

<%= link_to_remote 'Users', :url => { :controller => 'admin/users', :action => :index } %>

And then when I deploy to production I get errors such as:

ActionView::MissingTemplate (Missing template admin/users.erb in view path /rails/myapp/releases/20090203200508/app/views:):

In production it asks for the wrong template. It should be asking for the template app/views/admin/users/index.html.erb like it does in development.

I found if I add a route:

map.connect 'admin/users', :controller => 'admin/users', :action => 'index'

my link_to_remote calls do begin to work in both development and production.

I do not want to add a route for every single url I have in my admin. What's the solution?

Using Rails 2.2.2.

Thanks,

Also, forgot to mention.. I tried this:

map.connect 'admin/:controller/:action/:id'

But I didn't see any change.

Thanks,

I don't think you can supply :controller => "some_controller" in link_to_remote, only :action and :id are valid. I think you should embrace the resourceful way of doing things and add this to your routes.rb file:

map.resources :users, :controller => 'admin/users', :name_prefix => 'admin_'

This would then give you many clean named routes, one of which is admin_users_path which will link to what you want. Now you can put:

:url => admin_users_path

I have not tested this, but I'm pretty sure it will work.

I don't think you can supply :controller => "some_controller" in link_to_remote,

I have about half-a-dozen Rails apps that say otherwise :slight_smile:

only :action and :id are valid. I think you should embrace the resourceful way of doing things and add this to your routes.rb file:

I do not want resources. I just want routes that work without adding an entry for each url in routes.rb.

map.resources :users, :controller => 'admin/users', :name_prefix => 'admin_'

This would then give you many clean named routes, one of which is admin_users_path which will link to what you want. Now you can put:

:url => admin_users_path

I have not tested this, but I'm pretty sure it will work.

Yes, it will, but that's not what I want for this app. I just want my admin controllers in a folder, that's it.

Thanks all the same.

Ah if only merb's routes were in rails already

Anyway, what OTHER routes do you have in front of the default route?
Coz when you're using namespaces controllers, it matters. Aside from
this, DHH has made it pretty clear this isn't a great thing to do.

Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/

Having a single admin controller with 807345029 lines of code in it clearly isn't a great thing to do either.

No, but putting admin_ in front of your admin controllers is fine.

Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/

That's not very DRY.

So use a loop with send to map LOL. A class def is only ruby after all

Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/

Here's what I do in my app

   %w[wip businesses people journal microsites brief reports
search].each do |controller_name|      map.connect "admin/#{controller_name}/:action/:id", :controller
=> "admin_#{controller_name}"    end

I'm sure you could probably do something like this, too, tho it's
untested:

   %w[wip businesses people journal microsites brief reports
search].each do |controller_name|      map.connect "admin/#{controller_name}/:action/:id", :controller
=> "admin/#{controller_name}"    end