问题描述
如何向我的 Rails 3 应用程序添加路由,以便我拥有一个映射到接受多个参数的 RESTful 资源中的操作的 URL:
How do I add a route to my Rails 3 app which allows me to have a URL that maps to an action in a RESTful resource that accepts multiple parameters:
/modelname/compare/1234,2938,40395
然后在我的控制器中,我想访问这些 ID:
And then in my controller, I want to access these ids:
@modelname = Modelname.find(params[:modelname_ids])
到目前为止,我一直在尝试匹配 '/modelname/compare/:modelname_ids', :to =>'modelname#compare'
,但我不断收到 No route matching "/modelname/compare/4df632fd35be357701000005,4df632fd35be357701000005"
.有什么建议吗?
So far, I have been trying match '/modelname/compare/:modelname_ids', :to => 'modelname#compare'
, but I keep getting No route matches "/modelname/compare/4df632fd35be357701000005,4df632fd35be357701000005"
. Any suggestions?
推荐答案
你可以设置一个匹配任何东西的路由,然后在你的控制器中拆分参数:
You can setup a route that matches anything, then split the parameter inside your controller:
resources :modelname do
match 'compare/*path' => 'controller#compare_action'
end
# controller:
def compare_action
@modelname = Modelname.find(params[:path].split(','))
end
这篇关于以多个 id 作为参数的 Rails 3 自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!