开发者

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 control开发者_高级运维ler 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.


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

resource :projects do

would be written as

resource :projects, :controller=>:publish do 


Use scope rather than namespace when you want a scoped route but not a controller within a module of the same name.


If I understand you correct, you want this:

scope :manage do
  resources :projects, :only => [] do
    get 'publish' => 'publish#create'
    get 'unpublish' => 'publish#destroy'
  end
end

to poduce these routes:

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

Am I understanding your need correctly? If so, this is what Ryan is explaining.


I think what you want is this:

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

This does create the named routes as you wish(manage_projects...) but still call the controller ::Publish


It's a bit late but for anyone still struggling with this, you can add a leading slash to the controller name to pull it out of any existing namespace.

concern :lockable do
  resource :locks, only: [] do
    post "lock" => "/locks#create"
    post "unlock" => "/locks#destroy"
  end
end

Now if you include that concern anywhere (either namespaced or not) you will always hit the LocksController.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜