2013-03-01 154 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