I am new to Ruby and Rails.
I made a simple application where the index page diaplays a Cars table with columns for make, model, and type (sportscar, SUV, luxury).
I have made it so I can click a column header and sort by that column
I also added a form above the table with a checkbox for each car type where the user can check/uncheck the boxes and click ‘submit’ and the table will only show cars with the types that were checked.
What I want to be able to do is sort by some column, and then check some boxes/click submit and show the result with the list still sorted AND also filtered.
Right now, I can either sort or filter, but if I do one action my application forgets about the previous action.
What is the RESTful way to do this?
Should I keep the ‘settings’ in the session hash, and construct a URI from that every time the user makes a new HTTP request?
What’s wrong with including sort/filter parameters in the request URI? For example, when you submit the filter form for make of “honda”, the uri will be something like
/cars?filter[make]=honda. If you also include sort options, the uri could be/cars?filter[make]=honda&sort_by=make.Inside your controller, you could access these through the params hash:
You can generate these uri’s by including the parameters in path helpers in your views:
This way, @filter and @order will be “persisted” between requests unless you explicitly change them. (These links are not DRY… you should generate them dynamically).