parse_sort_param returning a nil

21 views
Skip to first unread message

Bob Sleys

unread,
Jul 27, 2011, 12:25:30 PM7/27/11
to hobo...@googlegroups.com
Based on the guides I've seen I should be able to do the following in my controller to setup sorting etc for table_plus.

    @spacelist = @floor.spaces.where(["name like ?", "%#{params[:search]}%"]).order(parse_sort_param(:name, :space_dwgid).join(" "))

However I kept getting a nil error and finally figured out that the first time to the page, IE before the used can click on the column to sort parse_sort_param was returning a nil so the join was failing.

I used the following as a work around

    sort = parse_sort_param(:name, :space_dwgid)
    sort||=["name","ASC"] #set a default sort order
    @spacelist = @floor.spaces.where(["name like ?", "%#{params[:search]}%"]).order(sort.join(" "))

Bob

Matt Jones

unread,
Jul 27, 2011, 12:40:23 PM7/27/11
to hobo...@googlegroups.com

This will result in table-plus not showing *any* column selected for sorting on an initial load - an alternative is to just fill the param manually:

params[:sort] ||= "name"
@spacelist = ... # parse_sort_param will never return nil now, and the column corresponding to 'name' will get the little sort arrow in the table-plus header

--Matt Jones

Bob Sleys

unread,
Jul 27, 2011, 1:53:10 PM7/27/11
to hobo...@googlegroups.com
Thanks great tip.  Perhaps this should be added to the book.

Bob
Reply all
Reply to author
Forward
0 new messages