重构路线 - 提供不同的布局

时间:2010-02-01 14:56:21

标签: ruby-on-rails layout views background

作为一个Rails NOOB,我开始使用routes.rb:

ActionController::Routing::Routes.draw do |map|
  map.resources :events
  map.connect 'affiliates/list', :controller => "affiliates", :action => "list"  
  map.connect 'affiliates/regenerate_thumb/:id', :controller => "affiliates", :action => "regenerate_thumb"
  map.connect 'affiliates/state/:id.:format', :controller => "affiliates", :action => "find_by_state"
  map.connect 'affiliates/getfeed', :controller => "affiliates", :action => "feed"
  map.resources :affiliates, :has_many => :events 

  map.connect ":controller/:action"
  map.connect '', :controller => "affiliates" 
  map.connect ":controller/:action/:id"
  map.connect ":controller/:action/:id/:format"
end

我试图收紧它。我已经达到了:

ActionController::Routing::Routes.draw do |map|
  map.resources :events, :only => "index" 
  map.resources :affiliates do |affiliates|
    affiliates.resources :has_many => :events
    affiliates.resources :collection =>  { :list => :get, :regenerate_thumb => "regenerate_thumb"   }
  end
  # map.connect 'affiliates/regenerate_thumb/:id', :controller => "affiliates", :action => "regenerate_thumb"
  map.connect 'affiliates/state/:id.:format', :controller => "affiliates", :action => "find_by_state"
  map.connect 'affiliates/getfeed', :controller => "affiliates", :action => "feed"
  map.root :affiliates

end

令我困惑的是路由与参数。例如,我意识到列表和索引之间的唯一区别是它是如何呈现的,而不是呈现的是什么。

采取不同的行动(就像我现在一样)感觉不对,但我无法弄清楚他是否正确。

由于

2 个答案:

答案 0 :(得分:0)

你可以像这样重构:

map.resources :events, :only => "index"
map.resources :affiliates, :has_many => :events, :collection => { :list => :get  }, :member => { :regenerate_thumb => :get } 
map.connect 'affiliates/state/:id.:format', :controller => "affiliates", :action => "find_by_state"
map.connect 'affiliates/getfeed', :controller => "affiliates", :action => "feed"
map.root :affiliates

答案 1 :(得分:0)

标题上写着“提供不同的布局”。问题出在哪里?