Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a flexible number of ids into a controller in Rails?

I'd like to display a comparison page so user can compare the properties of several objects in my database. Any number of objects can be compared. I'd also like it to be bookmarkable (so a 'get').

How should I structure my URL / route for the controller?

Something like /foo_compare/1_5_22 where I split the ids into 1, 5 and 22 in the controller?

Maybe /foo_compare/1/5/22, but how would I set up the route?

like image 790
RichH Avatar asked Feb 02 '26 23:02

RichH


2 Answers

I'd prefer

/compare?a=1&b=5&c=22

The 1_5_22 is just fugly, and I think that

/compare/1/5/22

is a very non-RESTful route.

like image 165
Daniel Lucraft Avatar answered Feb 05 '26 13:02

Daniel Lucraft


# routes.rb
map.connect 'compare/*:comparisons', :controller => 'whatever', :action => 'you_name_it'

# in the controller
ids = params[:comparisons].split('/')

This maps to e.g. /compare/1/5/203.

like image 26
August Lilleaas Avatar answered Feb 05 '26 14:02

August Lilleaas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!