Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Custom Route that takes multiple ids as a parameter

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?

like image 258
Avishai Avatar asked Jun 20 '11 14:06

Avishai


1 Answers

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
like image 67
jdeseno Avatar answered Oct 01 '22 02:10

jdeseno