2009-07-20 134 views
0

我正在寫一個會議列出的Rails的網站,以及跨越這個要求就來了:在Rails中路由鏈接?

鏈,在沒有特定的順序,一個URL找到的事件,如:

/中/:城市/上/:標籤/與/:揚聲器

或重新像

/中/:城市/與/:揚聲器/上/:標籤

我可以處理這些細微一個接一個。有沒有辦法動態處理這些請求?

routes.rb中:

map.connect '/:type/*facets', :controller => 'events', :action => 'facets' 

events_controller.rb:

def facets 
    @events = find_by_facets(params[:facets], params[:type]) 
    render :action => "index" 

    end 

application_controller.rb

回答

0

我具有以下(藉口在那裏一些特定的應用程序代碼)來實現它:

def find_by_facets(facets, type = nil) 
query = type.nil? ? "Event" : "Event.is('#{type.singularize.capitalize}')" 
for i in (0..(facets.length - 1)).step(2) 
    query += ".#{facets[i]}('#{facets[i+1].to_word.capitalize_words}')" 
end 
@events = eval(query) 

end 

event.rb:

named_scope :in, lambda { |city| { :conditions => { :location => city } } } 

    named_scope :about, lambda { 
     |category| { 
      :joins => :category, 
      :conditions => ["categories.name = ?", category] 
     } 
    } 

    named_scope :with, lambda { 
     |speaker| { 
      :joins => :speakers, 
      :conditions => ["speakers.name = ?", speaker] 
     } 
    } 

    named_scope :on, lambda { 
     |tag| { 
      :joins => :tags, 
      :conditions => ["tags.name = ?", tag] 
     } 
    } 

    named_scope :is, lambda { 
     |type| { 
      :joins => :type, 
      :conditions => ["types.name = ?", type] 
     } 
    } 

這給了我像URL /會議/中/ salt_lake_city /與/或joe_shmoe /講座/約/編程/與/ joe_splo /,沒有特定的順序。贏得!