开发者

Refactoring routes - serving different layouts

开发者 https://www.devze.com 2022-12-18 12:40 出处:网络
As a Rails NOOB, I started with a routes.rb of: ActionController::Routing::Routes.draw do |map| map.resources :events

As a Rails NOOB, I started with a routes.rb of:

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.con开发者_JAVA技巧nect '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

and i'm trying to tighten it up. and I've gotten as far as:

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

what is confusing to me is routes vs parameters.. For example, I realized that the only difference between list and index is HOW it is rendered, rather than WHAT is rendered.

Having a different action (as I do now) feels wrong but I can't figure out he right way.

Thanks


You can refactor it like this:

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


The title says "serving different layouts". Where's the problem?

0

精彩评论

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