Parsing arrays from globbed routes into controller


#1

I have this in routes.rb:

map.connect ‘pupils/*filters’, :controller => “pupils”, :action =>
“:filter”

Given this url, for example:

http://localhost:3000/pupils/cohort_id/1/house_id/3

I would like to be able to grab an unlimited number of values to
filter pupils in the controller (so I don’t have to write out all the
options in route.rb)

So far I have… (but clearly this isn’t going to work)

def filter
@pupils = Pupil.find(:all, :conditions => params[:filters])
end

I found this with google but it complains about an odd number of
arguments:

@pupils = Pupil.find(:all, :conditions => Hash[params[:filters]])

Please help me with the controller find line, or tell me this isn’t
the way to be doing this at all.

Thanks.


Matt


#2

I dont think this is the way to do this at all.

I have been told it is not wise to have very nested resources (2
maximun) so with a better design, you might come up with something
where you can do filtering in routes

map.resources :pupils, :has_many => {:houses, :cohorts, …}, :has_one
=> {:mansion, …}

this will give you routes like

pupils/2/cohorts/:cohort_id => will go to controller cohorts, action
show and params will contain :pupil_id => 2
pupils/2/cohorts => will go to controller cohorts, action index and
params will contain :pupil_id => 2
pupils/2/houses/:cohort_id => will go to controller houses, action
show and params will contain :pupil_id => 2

… etc

so you dont have to write ALL the possible routes, but you have to
route all the parameters you want to filter by. This is a solution to
what it seems your problem here, but i suggest you have a good think
in your design as to why you would need to filter pupils in the routes
and not in the index page of the pupils controller (for example) or
somewhere where it is more “appropiate” (easier).