2014-04-01 63 views
0

我今天遇到這個錯誤,我搜索谷歌的解決方案,但我找不到這個正確的答案。錯誤:未定義的方法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 

我對這個錯誤很迷茫,有人可以告訴我任何解決方案嗎?

+0

如果您將堆棧跟蹤與錯誤一起粘貼,那就太好了。原因是,某處某處調用了'nil'上的'id'方法。 – Amadan

+0

屏幕截圖錯誤在這裏:http://postimg.org/image/b2mwn3usp/希望它有幫助 – dailammoc

+1

Stack Overover之外的屏幕截圖被忽視,因爲它們可能會過期並破壞問題。此外,處理文本比圖像更容易。最後,它沒有堆棧跟蹤(儘管它確實有錯誤發生的代碼,所以很幸運)。 – Amadan

回答

0

您正在迭代屬性名稱@model,並在@crud上閱讀它們。 @model具有名爲id的屬性; @crudnil。兩者都應該是相同的(可能),或者在有些不尋常的情況下,您的意思是讀取@model的屬性爲@crud,您需要檢查@crud的分配位置。

0

你的代碼是有點混亂,一些小意見:

model.find_by_id(params[:id]) 

應該是:

model.find(params[:id]) 

接下來我會確保你的觀點實際上通過PARAMS [:ID]到視圖而不是像crud_id。如果後者是這種情況,你應該這樣稱呼:

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

最後,我認爲你應該添加:文件到你的強參數。

相關問題