Stefan
Stefan

Reputation: 42905

Submit check_box_tag values along with link_to parameters

Another one from the SaaS class after quite some trial and only error. There's a table with movies that can be filtered (by checking boxes) and sorted by ratings (by clicking on theader). If filtered, subsequent sorting should preserve the filter.

I guess the request triggered by 'link_to' should include the 'ratings' parameters in the _path() so the controller can query and submit to view accordingly.

If that's right - how do I get the check_box_tag values in the movies_path()? If not, what else should I do? Thanks in advance!

%h1 All Movies
=puts 'Ratings: ', params[:ratings].nil?
= form_tag movies_path, :id => 'ratings_form', :method => :get do
  Include:
  - @all_ratings.each do |rating|
    = rating
    = check_box_tag "ratings[#{rating}]", 1, @filtered_ratings.include?(rating) ? true : false
  = submit_tag 'Refresh', :id => 'ratings_submit'

%table#movies
  %thead
    %tr
      %th{:class => @title_header}= link_to "Movie Title", movies_path(:sort => "title"), :id => 'title_header'
      %th Rating
      %th{:class => @date_header}= link_to "Release Date", movies_path(:sort => "release_date"), :id=> 'release_date_header'
      %th More Info
  %tbody

Upvotes: 1

Views: 1657

Answers (1)

Stefan
Stefan

Reputation: 42905

So I learnt that one can simply add elements from params[] to the _path() so that these show up in the URL separated by ?. Hope this will be useful for other fellow beginners..

  %thead
    %tr
      %th{:class => @title_header}= link_to "Movie Title", movies_path(:sort => "title", :ratings => params[:ratings]), :id => 'title_header'
      %th Rating
      %th{:class => @date_header}= link_to "Release Date", movies_path(:sort => "release_date", :ratings => params[:ratings]), :id=> 'release_date_header'
      %th More Info
  %tbody

Upvotes: 1

Related Questions