Rails 3 Custom Route that takes multiple ids as a parameter

旧巷老猫 提交于 2019-11-30 07:02:46

问题


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

And then in my controller, I want to access these ids:

@modelname = Modelname.find(params[:modelname_ids])

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?


回答1:


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


来源:https://stackoverflow.com/questions/6413077/rails-3-custom-route-that-takes-multiple-ids-as-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!