【发布时间】:2013-06-06 13:15:34
【问题描述】:
我一直在尝试通过关注这些instructions 来对 jquery-datatables 进行过滤。
在我添加过滤器之前,它返回数据表所需的 json 没有任何问题,但现在我收到上述错误并且无法识别问题。
def index
respond_to do |format|
format.html
format.json do
@sbcons = Subcontractor.scoped
filters = params[:filter]
@sbcons = @sbcons.where(sbcon_type: filters[:type]) unless filters[:type].nil? or filters[:type].blank?
@sbcons = @sbcons.where(cscs_card: filters[:cscs]) unless filters[:cscs].nil? or filters[:cscs].blank?
@sbcons = @sbcons.where(approved_status: filters[:approved]) unless filters[:approved].nil? or filters[:approved].blank?
render json: SubcontractorsDatatable.new(view_context, @sbcons)
end
end
end
<% provide(:title, 'All Subcontractors') %>
<h1>Subcontractors List</h1>
<div class="filter">
<%= form_tag(method: :get, id: "filter_form") do %>
<%= label_tag :sbcon_type, "Type" %>
<%= select_tag "filter[type]", options_for_select([[],["Labour Only"], ["Specialist"], ["Both"]]) %>
<%= label_tag :cscs_card, "CSCS" %>
<%= select_tag "filter[cscs]", options_for_select([[],["Yes"], ["No"]]) %>
<%= label_tag :approved_status, "Approved Status" %>
<%= select_tag "filter[status]", options_for_select([[],["Approved"], ["Not Approved"]]) %> <br>
<%= link_to "Filter", '#', id: "filterbutton", class: "btn btn-mini" %>
<% end %>
<br>
</div>
<table id="subcontractors" class="table table-condensed table-hover display" data-source="<%= subcontractors_url(format: "json") %>">
<thead>
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>CSCS</th>
<th>Type</th>
<th>Scotland</th>
<th>NE England</th>
<th>NW England</th>
<th>Midlands</th>
<th>SE England</th>
<th>SW England</th>
<th>London</th>
<th>Wales</th>
<th>Operatives</th>
<th>Product Liability</th>
<th>Employer Liability</th>
<th>Public Liability</th>
<th>Contractors All Risk</th>
<th>Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<%= javascript_tag do %>
$('#filterbutton').click(function (){
$('#subcontractors').dataTable().fnDraw();
});
<% end %>
编辑:
NoMethodError(索引中未定义的方法[]' for nil:NilClass):
app/controllers/subcontractors_controller.rb:31:inblock(2 级)'
app/controllers/subcontractors_controller.rb:26:in `index'
第 31 行:@sbcons = @sbcons.where(sbcon_type: filters[:type]) unless filters[:type].nil?还是过滤器[:type].blank?
第 26 行:respond_to do |format|
【问题讨论】:
-
你能发布堆栈跟踪,它在哪一行显示错误
-
另外,
filters[:type].blank?也将测试.nil?:nil.blank? #=> true(而.present?是.blank?的反面) -
我已用跟踪更新了问题,并感谢有关 .nils 无关的信息。
-
filters在第 31 行为零。 -
不会,但
filters.nil?会。
标签: ruby-on-rails ruby-on-rails-3