views:

154

answers:

4

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?

+4  A: 
a) url_for(:controller => "my_controlelr",:action => "compare", :id => [1,2,3,4]) becomes "id"=>"1/2/3/4"

link would be my_controlelr/compare/1/2/3/4

b) url_for(:controller => "my_controlelr",:action => "compare", :ids => [1,2,3,4]) becomes "ids" => ["1", "2", "3", "4"]

link would be my_controlelr/compare/?ids[]=1&ids[]=2&ids[]=3&ids[]=4

No special routes are necessary

aivarsak
option (b) seems cleanest
Jordan Liggitt
+4  A: 
# 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.

August Lilleaas
+5  A: 

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.

Daniel Lucraft
I agree. It implies that 22 is nested inside 5, which is nested inside 1. Instead those IDs are all peers.
John Topley
I agree. I wasn't happy with either of my suggestions in the question. How would I got about allowing an unlimited list in this way? I'd hate to hard code a, b, c ... and what happens when I get to 27 comparison objects (I doubt a user would want to compare that many items, but you never know).
RichH
ARemesal
ARemesal
+1  A: 

While I don't like the underscores, I think a delimited approach is reasonable. I'd probably use ';'

Logically, this is a set of IDs, exposing them as a series of parameters isn't great unless you really want to enforce a limit on the number of them

Kevin Davis