I want to sort the items that have been previously selected with checkboxes. If I select ratings for movies by clicking the checkboxes, it returns the movies that apply.
The end of the URL looks like this:
?ratings[PG-13]=1&ratings[G]=1
If I click on the link that orders the movies, it takes the whole list of movies and not the ones that I just targeted. So, how can I “append” "sort=title" to the ratings in the URL?
I have a feeling it’s something I can do in the view. My link_to is this:
%th#title_header{:class=>("hilite" if @sort == "title")}= link_to "Movie Title", :sort => "title"
and my check_box_tag is this:
= check_box_tag "ratings[#{rating}]",rating,@all.include?(rating)
In my controller, I have this:
def index
@movies = Movie.order(params[:sort])
@movies = @movies.where(:rating => params[:ratings].keys) if params[:ratings].present?
@sort = params[:sort]
@all = (params[:ratings].present? ? params[:ratings] : [])
end
(I know, my “where” query is vulnerable to SQL injection, it’s temporary, I’m just trying to conceptualize the whole thing.)
You need to pass an extra hash at the end of link_to ,it chains the queries in the url:
Brilliant!