vendredi 23 octobre 2015

Remember state of checkboxes in a ransack searchform

I am building a searchform with ransack for a model called house. I am using checkboxes to search for all instances that meet one of the checked values for one certain attribute. It all works OK at the moment. My current goal is to make my application remember the state of all checkboxes. That's my current status:

<%= search_form_for @search, url: houses_list_path, :builder => SimpleForm::FormBuilder do |f| %>
    <% House.pluck(:n_people).to_a.uniq.reject!{|a| a == nil}.each do |n| %>
      <%= check_box_tag('q[n_people_eq_any][]', n, (params[:q][:n_people_eq_any].include?(n.to_s)) %>
      <%= n %>
    <% end %>

The problem with this code is that if I open the site without any search params in my url there is no params[:q][:n_people_eq_any] and I will get an error. I tried to put if statements into the check_box_tag but it's not working. I bet there is an easy way to check if the param is there but I can't figure it out.

Hope someone can give me a hand on this.




Aucun commentaire:

Enregistrer un commentaire