2014-04-01 6 views
0

오늘이 오류가 발생했습니다. Google에서 솔루션을 검색하지만 이에 대한 정답을 찾을 수 없습니다. 오류 :이 templete입니다 파일입니다정의되지 않은 메소드 'id'for nil : NilClass (NoMethodError)

NoMethodError at /master/hotels/import 

undefined method `id' for nil:NilClass 

:이 오류에 대한 매우 혼란 스러워요

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 

: 여기

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' 

및 내 컨트롤러 파일입니다 누군가가 내게 어떤 해결책을 말해 줄 수 있습니까?

+0

오류와 함께 스택 추적을 붙여 넣으면 좋을 것입니다. 원인은, 어딘가에서,'nil'에 대해'id' 메소드를 호출했습니다. – Amadan

+0

스크린 샷 오류가 있습니다. http://postimg.org/image/b2mwn3usp/ 도움이 되길 바랍니다. – dailammoc

+1

Stack Overflow 외부에서 호스팅되는 스크린 샷은 유효 기간이 만료되어 질문을 깰 수 있으므로 눈살을 찌푸 릅니다. 또한 이미지보다 텍스트를 처리하는 것이 더 쉽습니다. 마지막으로 스택 추적이 없었습니다 (오류가 발생하는 코드가 있어도 운이 좋았습니다). – Amadan

답변

0

@model의 속성 이름을 반복하고 @crud에서 읽는 중입니다. @model에는 id이라는 속성이 있습니다. @crudnil입니다. 둘 다 동일해야합니다 (아마도), 또는 @model의 속성을 @crud으로 읽는 것을 의미하는 다소 이상한 경우에는 @crud이 할당되어 있는지 확인해야합니다.

model.find_by_id(params[:id]) 

가되어야한다 : [: ID] 뷰에

model.find(params[:id]) 

다음은 내가보기 실제로 PARAMS를 전달해야 할 것

0

코드는, 몇 가지 작은 코멘트를 조금 혼란 crud_id와 같은 것이 아닙니다. 후자의 경우라면 다음과 같이 호출해야합니다.

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

마지막으로, 강력한 매개 변수에 : file을 추가해야한다고 생각합니다.

관련 문제