2013-03-01 150 views
0

我使用「mongoid_search」在我的應用程序的索引頁上進行搜索。Rails:根據url顯示搜索結果

的URL,它返回的結果看起來是這樣的: 「?/型材UTF8 =✓& profile_search =聖+安東尼奧%2C + TX」

我將如何去獲得/位置/聖安東尼奧馬刺-TX返回與搜索網址相同的結果?

基本上,「位置」之後的任何內容都應該轉換爲「profile_search」查詢字符串。

另外,是否可以保持URL相同? (例如:'/ location'san-antonio-tx「url不會改變爲查詢字符串)

+0

您是否希望從[http://example.com/location/sanantonio-tx](http://example.com/location/sanantonio-tx)轉到[http://example.com/profiles?utf8 =✓&profile_search =聖安東尼奧+%2C + TX](http://example.com/profiles?utf8=✓&profile_search=San+Antonio%2C+TX)?而「保持URL相同」是什麼意思? – adamdunson 2013-03-01 05:09:39

回答

1

查看您提供的代碼,似乎是在」配置文件「的」索引「操作中執行搜索, 。控制器如果是這樣的話,你可以創建一個新的路徑:

#config/routes.rb 
match '/location/:profile_search' => 'profiles#index' 

這將調用的「配置文件」控制器「指數」行動,同時提供所有的「/位置/」部分之後的作爲名爲「profile_search」的參數的URL

欲瞭解更多信息:http://guides.rubyonrails.org/routing.html

+0

謝謝almathie。這就是我一直在尋找的。儘管如此,我確實需要改進它。例如,「/ location/san-antonio-tx」應在「profile_search」中針對三個關鍵字(san,antonio和tx)進行搜索。用你提供的代碼搜索「san-antonio-tx」作爲一個關鍵字。關於如何讓這個工作的任何想法? – sevens 2013-03-01 12:38:36

+0

好吧,看起來我回答了我自己的問題。將「 - 」放在URL中的單詞之間使其起作用。 「san-antonio-tx」將被處理爲3個關鍵字。再次感謝almathie。 – sevens 2013-03-01 12:44:25