nil的未定义方法`update_all':NilClass

时间:2015-04-17 11:39:46

标签: ruby-on-rails haml

我正在尝试实现一种通过复选框更新我的条目的方法,以便通过复选框可以清除所选条目,并在单独的视图上呈现完整的条目。我通过复选框跟踪railscast更新条目,但我收到上述错误,我似乎无法解决它。

控制器:

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

1 个答案:

答案 0 :(得分:1)

你应该使用

Diagnostic.where(params[:diagnostics_ids]).update_all(completed_at: Time.now)