Best Way to Get End-User Specify Sort Order in Rails

I'm looking for a suggestion on how an end user from files in the form of a Rails application sets the sort order of the result set returned by the find model method. In other words, I would like the user to be able to select their sort order from the selection list.

At first, I thought I could just put the line that I would put in the: order parameter, but that seems like a bad idea from a security point of view.

I suppose I could always use a switch based on the values ​​from the selection list, but that seems a bit cumbersome.

Thanks for watching.

+6
sorting ruby-on-rails interface
source share
3 answers

I would use AR :: Base # column_names to sanitize input. Something like:

@models = Model.find(:all, :order => params[:sort].select({|name| Model.column_names.include? (name) } ).join(',') ) 

You can expand this with a bit of preprocessing to vary whether you want to sort the up or down with each key. Hope this helps!

+4
source share

This may be beyond what you are looking for, but lately I have relied on javascript to take care of the subsequent sorting for me. A good prototype table sorter is Tablekit ( http://www.millstream.com.au/view/code/tablekit ), which is unobtrusive, fast and easy to use. It also includes subtleties such as in-place editing and column resizing.

+2
source share

something rails can copy from cakephp framework (paginator sorter by index () in cakephp)

0
source share

All Articles