Ransack显示所有而不是过滤器

时间:2018-02-04 06:53:16

标签: ruby-on-rails ransack

我正在尝试使用Ransack gem,但它返回所有12,000行而不是使用过滤器。

控制器:



	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>
&#13;
&#13;
&#13;

在seraching后,URL看起来没问题: URL

但它表现得像:

&#13;
&#13;
@computers = LmiHost.all
&#13;
&#13;
&#13;

任何想法也是为什么会发生这种情况?

1 个答案:

答案 0 :(得分:3)

你做错了是你没有指定一个合适的匹配器。如果要选择特定的作业ID,则应在表单中指明搜索匹配器。

例如,您说的是job_id应该是job_id_eq或者您想要它的匹配器

你应该获得如下的Ransack搜索项目。

Ransack::Search<class: LmiHost, base: Grouping <conditions: [Condition <attributes: ["job_id"], predicate: eq, values: ["123"]>], combinator: and>> 
在你的Ransack搜索中,我没有看到谓词,因此它会返回所有的结果。