问题描述
如何向我的Rails 3应用添加路由,该路由允许我拥有一个URL,该URL映射到RESTful资源中接受多个参数的动作:
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’
,但我不断收到没有路线匹配 / 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 Custom Route的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!