Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Routing - specifying an exact controller from within a namespace

I'm having a bit of a problem with route namespaces that I've not encountered before. This is actually a part of some gem development I'm doing - but i've reworked the problem to fit with a more generic rails situation.

Basically, I have a namespaced route, but I want it to direct to a generic (top-level) controller.

My controller is PublishController, which handles publishing of many different types of Models - which all conform to the same interface, but can be under different namespaces. My routes look like this:

# config/routes.rb

namespace :manage do
  resources :projects do
    get 'publish' => 'publish#create'
    get 'unpublish' => 'publish#destroy'
  end
end

The problem is that this creates the following routes:

manage_project_publish GET    /manage/projects/:project_id/publish(.:format)        {:controller=>"manage/publish", :action=>"create"}
manage_project_unpublish GET    /manage/projects/:project_id/unpublish(.:format)      {:controller=>"manage/publish", :action=>"destroy"}

Which is the routes I want, just not mapping to the correct controller. I've tried everything I can think of try and allow for the controller not to carry the namespace, but I'm stumped.

I know that I could do the following:

get 'manage/features/:feature_id/publish' => "publish#create", :as => "manage_project_publish"

which produces:

manage_project_publish GET    /manage/projects/:project_id/publish(.:format)        {:controller=>"publish", :action=>"create"}

but ideally, I'd prefer to use the nested declaration (for readability) - if it's even possible; which I'm starting to think it isn't.

like image 675
theTRON Avatar asked Apr 11 '11 00:04

theTRON


1 Answers

resource takes an optional hash where you can specify the controller so

resource :projects do

would be written as

resource :projects, :controller=>:publish do 
like image 68
masukomi Avatar answered Sep 20 '22 03:09

masukomi