2010-07-10 29 views
0

我有一個在Rails中創建的在線組合,其中包含不同的項目。我希望能夠通過關鍵字過濾項目。我的方法是爲ProjectsController中的每個關鍵字定義一個方法,並鏈接關鍵字以調用方法。在Controller中調整方法調用的routes.rb

對於實例關鍵字= graphic_design:

<%= link_to 'Graphic Design', :action => "filter_"[email protected]_s %> 

這樣,我得到的錯誤:

Couldn't find Project with ID=filter_graphic_design 

這是我相當明顯。我的問題:有沒有辦法告訴routes.rb行爲只有'filter_'方法不同?還有其他建議嗎?

回答

1

你的做法是錯誤的沒有試過。爲什麼您首先需要針對每個關鍵字的filter_方法?它非常簡單的解決方案。首先在你的routes.rb定義了一個名爲路線:

map.filter '/projects/:filter_this_for_me', :controller => 'projects', :action => 'filter' 

在您的看法,

<%= link_to 'Graphic Design', filter_path("filter_" + @project.keyword.to_s) %> 

在您的篩選器操作,

def filter 
    logger.info("Parameters that is being received: #{params}") 
    filter_what = params[:filter_this_for_me] 

    if(!filter_what.nil? && !filter_what.blank?) 
     # Here filter_what will have "filter_graphic_design" or "filter_something" 
     # With which you can filter any data that you want. 
     # Filter your projects here. 
    end 
end 
1

我覺得這樣的事情可以工作

map.connect "/projects/filter_{filter}", :controller => 'projects', :action => 'filter' 

雖然

相關問題