未定义的方法`id' for nil:NilClass(NoMethodError)

时间:2014-04-01 02:41:32

标签: 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), 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

我对此错误感到非常困惑,有人可以告诉我任何解决方案吗?

2 个答案:

答案 0 :(得分:0)

您正在重复@model的属性名称,并在@crud上阅读它们。 @model有一个名为id的属性; @crudnil。两者都应该是相同的(可能),或者在某种不寻常的情况下,您的意思是从@model读取@crud的属性,您需要检查@crud的位置已分配。

答案 1 :(得分:0)

您的代码有点令人困惑,有些小评论:

model.find_by_id(params[:id])

应该是:

model.find(params[:id])

接下来我会确保你的视图实际上将params [:id]传递给视图而不是像crud_id那样。如果是后者,你应该这样称呼:

model.find_by(crud_id: params[:crud_id])

最后,我认为您应该将:file添加到您的强参数中。