Rails 3路由单控制器和多个资源

时间:2012-12-28 20:24:43

标签: ruby-on-rails ruby-on-rails-3 routes

我有多个资源(:countries, :states, :schools等),但想要一个“Dashboard”控制器来处理所有操作。

我希望能够做到以下几点:

countries_path会引导我在show_countries中执行DashboardController操作,并可由'/dashboard/countries访问。

同样适用于州,学校等。

我已经阅读了Rails路由,并且一直在搞乱各种选项。我最终在routes.rb文件中找到了以下内容:

scope "toolbox" do
  resources :countries, :controller => "toolbox", :only => :index do
    get 'show_countries', :on => :collection
  end

  ...
end

运行rake routes为上面的代码提供了以下内容:

show_countries_countries GET    /toolbox/countries/show_countries(.:format)  {:action=>"show_countries", :controller=>"toolbox"}
countries GET    /toolbox/countries(.:format)  {:action=>"index", :controller=>"toolbox"}

我试过这个:

scope "toolbox" do
  resources :countries, :controller => "toolbox", :only => :index, :action => "show_countries"
end

只是为了得到这条路线:

countries GET    /toolbox/countries(.:format)  {:action=>"index", :controller=>"toolbox"}

我真正想要的是:

countries GET    /toolbox/countries(.:format)  {:action=>"show_countries", :controller=>"toolbox"}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你只需要在'资源'框之外思考:

scope "toolbox", :controller => :toolbox do
  get 'countries' => :show_countries
  get 'states' => :show_states
  get 'schools' => :show_shools
end

应该输出这样的路线:

countries GET /toolbox/countries(.:format) toolbox#show_countries