未定义的方法ID为零:NilClass(NoMethodError)
问题描述:
我今天遇到这个错误,我搜索谷歌的解决方案,但我找不到这个正确的答案。错误:未定义的方法ID为零:NilClass(NoMethodError)
NoMethodError at /master/hotels/import
undefined method `id' for nil:NilClass
这是templete文件:
row
.col-xs-12
%p.pull-left
%button.btn.btn-white.btn-sm
一括削除
%p.pull-right
= link_to "新規作成", url_for(action: :new), class: "btn btn-white btn-sm"
= link_to "CSV Export", url_for(action: :index, format: 'csv'), class: "btn btn-white btn-sm"
= link_to "CSV Upload", url_for(action: :import), class: "btn btn-white btn-sm"
-#= form_tag url_for(action: :import), class: 'pull-right' do |f|
-#= file_field_tag :csv, as: :file
-#= submit_tag "CSV Upload", input_html: {class: "btn btn-white btn-sm"}
.col-xs-12
= render 'cruds/grid'
,这里是我的控制器文件:
class CrudsController < ApplicationController
before_action :load_crud
before_action :set_crud, only: [:show, :edit, :update, :destroy]
def load_crud
end
# GET /cruds
# GET /cruds.json
def index
@items_grid = initialize_grid(@model)
@csv = CSV.generate() do |csv|
csv << @model.column_names
@model.all.each do |item|
csv << item.attributes.values_at(*@model.column_names).map{|i| i.to_s.encode("cp932", "UTF-8")}
end
end
respond_to do |format|
format.html { render template: 'cruds/index'}
format.csv { send_data @csv }
#format.xls # {send_data @product.to_csv (col_sep: "|t")}
end
# render template: 'cruds/index'
end
# GET /cruds/1
# GET /cruds/1.json
def show
render template: 'cruds/show'
end
# GET /cruds/new
def new
@crud = @model.new
render template: 'cruds/new'
end
# GET /cruds/1/edit
def edit
render template: 'cruds/edit'
end
# POST /cruds
# POST /cruds.json
def create
@crud = @model.new(crud_params)
respond_to do |format|
if @crud.save
format.html { redirect_to [:master, @crud], notice: 'Crud was successfully created.' }
format.json { render action: 'show', status: :created, location: @crud }
else
format.html { render action: 'new' }
format.json { render json: @crud.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /cruds/1
# PATCH/PUT /cruds/1.json
def update
respond_to do |format|
if @crud.update(crud_params)
format.html { redirect_to [:master, @crud], notice: 'Crud was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @crud.errors, status: :unprocessable_entity }
end
end
end
# DELETE /cruds/1
# DELETE /cruds/1.json
def destroy
@crud.destroy
respond_to do |format|
format.html { redirect_to action: :index }
format.json { head :no_content }
end
end
def import
@model.import(params[:file])
redirect_to root_url, notice: "Products imported."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_crud
@crud = @model.find_by_id(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def crud_params
params[@hash].permit(@model.attribute_names)
end
末
我对这个错误很迷茫,有人可以告诉我任何解决方案吗?
答
您正在迭代属性名称@model
,并在@crud
上阅读它们。 @model
具有名为id
的属性; @crud
是nil
。两者都应该是相同的(可能),或者在有些不寻常的情况下,您的意思是读取@model
的属性为@crud
,您需要检查@crud
的分配位置。
答
你的代码是有点混乱,一些小意见:
model.find_by_id(params[:id])
应该是:
model.find(params[:id])
接下来我会确保你的观点实际上通过PARAMS [:ID]到视图而不是像crud_id。如果后者是这种情况,你应该这样称呼:
model.find_by(crud_id: params[:crud_id])
最后,我认为你应该添加:文件到你的强参数。
如果您将堆栈跟踪与错误一起粘贴,那就太好了。原因是,某处某处调用了'nil'上的'id'方法。 – Amadan
屏幕截图错误在这里:http://postimg.org/image/b2mwn3usp/希望它有帮助 – dailammoc
Stack Overover之外的屏幕截图被忽视,因为它们可能会过期并破坏问题。此外,处理文本比图像更容易。最后,它没有堆栈跟踪(尽管它确实有错误发生的代码,所以很幸运)。 – Amadan