Mongoid 无效查找错误 rails
Mongoid invalid find error rails
我知道这个错误来自我的控制器中的 find 方法,但我不太确定如何修复错误。我正在尝试创建一个完整的方法,以便可以通过复选框将帖子存档并从索引页面中删除。目前的路由在相关的完整管理诊断中还没有 :id 所以我想知道这是否也是一个问题。我也在使用 rails 3.
错误
Mongoid::Errors::InvalidFind in Admin::DiagnosticsController#complete
Problem:
Calling Document.find with nil is invalid.
Summary:
Document.find expects the parameters to be 1 or more ids, and will return a single document if 1 id is provided, otherwise an array of documents if multiple ids are provided.
Resolution:
Most likely this is caused by passing parameters directly through to the find, and the parameter either is not present or the key from which it is accessed is incorrect.
查看
%h1 Diagnostics
%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 'A Checkbox', method: :put, data: { confirm: "Are you sure?" }
= link_to 'Save', complete_admin_diagnostics_path, method: :put, data: { confirm: "Are you sure?" }
= paginate @diagnostics
控制器
class Admin::DiagnosticsController < Admin::BaseController
before_filter :diagnostic, :except => [:index]
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_attribute(:completed_at, Time.now)
redirect_to @diagnostics, notice: "Todo item completed"
return
end
# def complete
# DiagnosticInfo.update_all(["Archive=?", Time.now], :id => params[:diagnostic_ids])
# params[:diagnostic_ids]
# # redirect_to admin_diagnostics_path
# end
private
def diagnostic
@diagnostic = DiagnosticInfo.find(params[:id])
end
end
请检查 params[:id]
不是 nil
我知道这个错误来自我的控制器中的 find 方法,但我不太确定如何修复错误。我正在尝试创建一个完整的方法,以便可以通过复选框将帖子存档并从索引页面中删除。目前的路由在相关的完整管理诊断中还没有 :id 所以我想知道这是否也是一个问题。我也在使用 rails 3.
错误
Mongoid::Errors::InvalidFind in Admin::DiagnosticsController#complete
Problem:
Calling Document.find with nil is invalid.
Summary:
Document.find expects the parameters to be 1 or more ids, and will return a single document if 1 id is provided, otherwise an array of documents if multiple ids are provided.
Resolution:
Most likely this is caused by passing parameters directly through to the find, and the parameter either is not present or the key from which it is accessed is incorrect.
查看
%h1 Diagnostics
%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 'A Checkbox', method: :put, data: { confirm: "Are you sure?" }
= link_to 'Save', complete_admin_diagnostics_path, method: :put, data: { confirm: "Are you sure?" }
= paginate @diagnostics
控制器
class Admin::DiagnosticsController < Admin::BaseController
before_filter :diagnostic, :except => [:index]
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_attribute(:completed_at, Time.now)
redirect_to @diagnostics, notice: "Todo item completed"
return
end
# def complete
# DiagnosticInfo.update_all(["Archive=?", Time.now], :id => params[:diagnostic_ids])
# params[:diagnostic_ids]
# # redirect_to admin_diagnostics_path
# end
private
def diagnostic
@diagnostic = DiagnosticInfo.find(params[:id])
end
end
请检查 params[:id]
不是 nil