我正在尝试使用 Ransack gem,但它返回所有 12,000 行而不是使用过滤器。
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
看法:
<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>
搜索后 URL 看起来没问题:
URL
但它的行为类似于:
@computers = LmiHost.all
任何想法为什么会发生这种情况?
最佳答案
你做错了什么是你没有指定一个合适的匹配器。如果您想选择特定的工作 ID,您应该在表单中指明搜索匹配器。
例如你说 job_id
它应该在哪里job_id_eq
或者你想要的匹配器
你应该在那里获得一个 Ransack Search 项目,如下所示。
Ransack::Search<class: LmiHost, base: Grouping <conditions: [Condition <attributes: ["job_id"], predicate: eq, values: ["123"]>], combinator: and>>
在您的 Ransack 搜索中,我没有看到谓词,因此它返回所有结果。
关于ruby-on-rails - Ransack 显示全部而不是过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48605566/