ruby on rails - How to use where clause based on parameters -


im trying have user able enter multiple fields can filter database . right have inputs being dynamically created when user selects column want search for.

the form submits url

/sites.csv?zipcode=74656?state=newyork. 

there may multiple of same fields example

/sites.csv?zipcode=78656?zipcode=74656?.  

i want able fields have zipcode 78656 , 74656 or state new york , zipcode 74656. whatever user enters , doesnt matter how many fields sent.

how can these parameters , clause each one

sites controller

class sitescontroller < applicationcontroller     def index         // if parameters exist           // clause         else          @sites = site.all        respond_to |format|                 format.html                 format.csv { send_data  // if fields exist  dont use sites @sites.to_csv }                 format.xls          end     end     private      def site_params         params.require(:site).permit(:latitude, :longitude, :building_height, :zoning_class, :state, :town, :zipcode, :county, :first_name, :last_name, :company_name)     end end 

index page

    = form_tag("/sites.csv", method: :get)             #csv-modal.modal.fade{"aria-labelledby" => "mymodallabel", :role => "dialog", :tabindex => "-1"}                 .modal-dialog                     .modal-content                         .modal-header                             %button.close{"aria-label" => "close", "data-dismiss" => "modal", :type => "button"}                                 %span{"aria-hidden" => "true"} &times;                             %h4.modal-title download csv                         .modal-body                             .btn-group                                 %button.btn.btn-default.btn-sm.dropdown-toggle{"aria-expanded" => "false", "aria-haspopup" => "true", "data-toggle" => "dropdown", :type => "button"}                                     add fields                                     %span.caret                                 %ul.dropdown-menu.csv-selector                                     %li                                         %a                                             latitude                                     %li                                         %a                                             longitude                                     %li                                         %a                                             state                                     %li                                         %a                                             town                                     %li                                         %a                                             zipcode                                     %li                                         %a                                             county                                     %li                                         %a                                             building height                                     %li                                         %a                                             zoning class                                     %li                                         %a                                             first name                                     %li                                         %a                                             last name                                     %li                                         %a                                             company name                              .input_fields_wrap                                 %div                         .modal-footer                             %button.btn.btn-default{"data-dismiss" => "modal", :type => "button"} close                             %button.btn.btn-default{:type => "submit"} download csv  :javascript      $(document).ready( function () {          $('.table').datatable();          var max_fields = 10; //maximum input boxes allowed         var wrapper = $(".input_fields_wrap"); //fields wrapper         var add_button      = $(".add_field_button"); //add button id          var x = 1; //initlal text box count         $(".csv-selector > li > a").click(function(e){ //on add input button click             e.preventdefault();             if(x < max_fields){ //max input box allowed                 x++; //text box increment                 $(wrapper).append('<div><input type="text" name="'+$(this).text().trim().tolowercase()+ '" class="form-control small-field" placeholder="'+$(this).text().trim()+'"/><a href="#" class="remove-field btn btn-danger remove-field">remove</a></div>'); //add input box             }         });          $(wrapper).on("click",".remove_field", function(e){ //user click on remove text             e.preventdefault(); $(this).parent('div').remove(); x--;         })     }); 

when same url parameter used, rails combines values in array , assigns value of parameter in parameter hash. hence request /sites.csv?zipcode=78656?zipcode=74656?, params be:

params[:zipcode] #=> [78656, 74656] 

when using where, activerecord builds in condition array of values can pass in params hash directly:

class sitescontroller < applicationcontroller     def index       @sites = site.where(params) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -