Ruby on rails 4 未定义的方法'id';对于nil:NilClass(nomethoderor)
我今天遇到了这个错误,我在谷歌搜索解决方案,但我找不到正确的答案。错误:Ruby on rails 4 未定义的方法'id';对于nil:NilClass(nomethoderor),ruby-on-rails-4,Ruby On Rails 4,我今天遇到了这个错误,我在谷歌搜索解决方案,但我找不到正确的答案。错误: NoMethodError at /master/hotels/import undefined method `id' for nil:NilClass 这是模板文件: row .col-xs-12 %p.pull-left %button.btn.btn-white.btn-sm 一括削除 %p.pull-right = link_to "新規作成", url_for(action: :new),
NoMethodError at /master/hotels/import
undefined method `id' for nil:NilClass
这是模板文件:
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
class CrudsController csv您正在迭代@model
的属性名称,并在@crud
上读取它们@model
有一个名为id
的属性<代码>@crud
为nil
。或者两者应该相同(可能),或者在某种不寻常的情况下,您确实想从@crud
中读取@model
的属性,您需要检查@crud
的分配位置。您的代码有点混乱,一些小注释:
model.find_by_id(params[:id])
应该是:
model.find(params[:id])
接下来,我要确保您的视图实际将params[:id]传递给视图,而不是像crud_id这样的东西。如果是后者,您应该调用:
model.find_by(crud_id: params[:crud_id])
最后,我认为您应该在强参数中添加:file。如果将stacktrace与错误一起粘贴,那就太好了。原因是,某个地方调用了
nil
上的id
方法。此处的屏幕截图错误:希望它能帮助对堆栈溢出之外的屏幕截图表示不满,因为它们可能会过期并打破问题。此外,处理文本比处理图像更容易。最后,它没有堆栈跟踪(即使它有发生错误的代码,所以很幸运)。