[英]undefined method `update_all' for nil:NilClass
我正在嘗試實現一種通過復選框更新條目的方法,以便可以通過復選框清除所選條目,並在單獨的視圖上呈現已完成的條目。 我已經遵循通過復選框更新條目的方法,但是遇到了以上錯誤,但似乎無法解決。
控制器:
class Admin::DiagnosticsController < Admin::BaseController
before_filter :diagnostic, :except => [:index, :complete]
def index
@diagnostics = DiagnosticInfo.all.order_by(:created_at.desc).page(params[:page]).per(50)
end
def show
respond_to do |format|
format.html
format.json { render json: @diagnostic }
end
end
def update
if @diagnostic.update_attributes(params[:diagnostic_info])
redirect_to admin_diagnostic_path, notice: 'Successfully updated.'
else
render action: "edit"
end
end
def edit
end
def destroy
diagnostic.destroy
redirect_to admin_diagnostics_path
end
def complete
@diagnostic.update_all(["completed_at=?", Time.now], :id => params[:diagnostics_ids])
end
private
def diagnostic
@diagnostic = DiagnosticInfo.find(params[:id])
end
end
視圖:
%h1 Diagnostics
= form_tag complete_admin_diagnostics_path, :method => :put
/ - for diagnostic in @diagnostics do
%table
%tr
%th
%th User
%th Message
%th Device
%th RELS-Mobile Version
%th Submitted
%th Archive
- @diagnostics.each do |diagnostic|
%tr
%td
%strong= link_to 'show', admin_diagnostic_path(diagnostic)
%td
- if diagnostic.user
= link_to diagnostic.user.email, [:admin, diagnostic.user]
- else
unknown
%td
= diagnostic.data["message"]
%td
%pre= JSON.pretty_generate(diagnostic.data["device"])
%td
= diagnostic.data["appVersion"]
%td
= diagnostic.updated_at
%td
= check_box_tag "diagnostic_ids[]", diagnostic.id
= diagnostic.data
= submit_tag "Mark as complete"
= paginate @diagnostics
你應該用
Diagnostic.where(params[:diagnostics_ids]).update_all(completed_at: Time.now)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.