简体   繁体   中英

Ransack showing all instead of filter

I am trying to use Ransack gem but it returns all 12,000 rows instead of using the filter.

Controller:

  def list @q = LmiHost.ransack(params[:q]) if !(params[:commit].nil?) then @computers = @q.result else @computers = LmiHost.where(:job_id => 121) end end 

View:

 <script type="text/javascript"> $(document).ready(function() { $("#computer_search_results").dataTable(); }); </script> <table id="computer_search_table"> <%= search_form_for @q do |f| %> <tr><td>Project:</td><td><%= f.select :job_id, @project_job_selector %></td></tr> <tr><td><%= f.submit 'Search' %></td></tr> <%end%> </table> <table id="computer_search_results"> <thead> <tr> <th>Lmi Name</th> <th>Service Tag</th> <th>Model</th> <th>Ship Date</th> <th>Warranty Date</th> <th>Project</th> <th>Invoice Name</th> </tr> </thead> <tbody> <% @computers.each do |computer| %> <tr> <td><%= computer.host_description %></td> <td><%= computer.host_service_tag %></td> <td><%= computer.host_model %></td> <td><%= computer.ship_date %></td> <td><%= computer.warranty_date %></td> <td><%= computer.job.name unless computer.job.nil? %></td> <td><%= computer.invoice_name %></td> </tr> <% end %> </tbody> </table> 

the URL looks okay after seraching: URL

But it is acting like :

 @computers = LmiHost.all 

Any ideas as too why this is happening?

What you are doing wrong is that you are not specifying a proper matcher. if you want to select particular job id you should indicate the search matcher in the form.

for example you are saying job_id where it should be job_id_eq or what ever matcher you want it to be

where you should getter a Ransack Search item like following.

Ransack::Search<class: LmiHost, base: Grouping <conditions: [Condition <attributes: ["job_id"], predicate: eq, values: ["123"]>], combinator: and>> 

in your Ransack search I don't see the predicate hence it returns all the resutls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM