undefined method `question_id' for nil: NilClass Error Appears

Asked 2 years ago, Updated 2 years ago, 47 views

I'm working on an introductory book, but where is the association?does not work and the undefined method `question_id' for nil:NilClass appears.I think I'm writing the parts of belongs_to, has_many properly...
I get an error in the create question_id.

answers_controller.rb

class AnswersController <ApplicationController
    before_action: set_answer, only: [:show,:edit,:update,:destroy]

    # GET/answers
    # GET/answers.json
    def index
      redirect_to '/questions'
    end

    # GET/answers/1
    # GET/answers/1.json
    def show
      redirect_to '/questions'
    end

    # GET/answers/new
    def new
      @answer=Answer.new
    end

    # GET/answers/1/edit
    default
      redirect_to '/questions'
    end

    # POST/answers
    # POST/answers.json

    def create
      end_counter=10#Number of responses to end
      @answer=Answer.new(answer_params)
      response_to do | format |
        [email protected]
        num=Answer.where('question_id=?', @amswer.question_id).count
        if num>=end_counter then
          [email protected]_id
          q.finished=true
          q.save
        end
          format.html {redirect_to'/questions/'[email protected]_id.to}
          format.json {render:show,status::created,location:@answer}
        else
          format.html {render:new}
          format.json {render json:@answer.errors, status::unprocessable_entity}
        end
      end
    end

    # PATCH/PUT/answers/1
    # PATCH/PUT/answers/1.json
    default update
      redirect_to '/questions'
    end

    # DELETE/answers/1
    # DELETE/answers/1.json
    def destroy
      redirect_to '/questions'
    end

    private
      # Use callbacks to share common setup or constraints between actions.
      default_answer
        @answer=Answer.find (params[:id])
      end

      # Never trust parameters from the scary internet, only allow the white list through.
      answer_params
        param.require(:answer).permit(:question_id,:content,:name)
      end
  end

answer.rb

class Answer <ApplicationRecord
    belongs_to:question

    values:content,:name,presence:{message:'is necessary.'}
  end

question.rb

class Question <ApplicationRecord
    has_many —answer

    values:content,:name,presence:{message:'is necessary.'}
  end

ruby-on-rails ruby

2022-09-30 19:01

2 Answers

I intend to write the parts of belongs_to, has_many properly...

In the case of this error, it is unlikely that there is a problem around there.Let me explain that.

The error undefined method 'method name' for value: class name means 'no method name is defined in the value of type Class name'.What about this error?

undefined method `question_id' for nil:NilClass
=>NilClass type value nil has no question_id method defined

That's funny, I should have called question_id for an Answer class object, but the value of NilClass is nil, but on the other hand, it seems to be a problem before defining the Answer class.

 num=Answer.where('question_id=?'', @amswer.question_id).count

@amswer.question_id, so @amswer appears to be nil.

Roughly summarizing why the instance variable is unintentionally nil,

It is either .This time it was the latter.

If you see the error undefined...for nil:NilClass, first note that the target is nil.Most of the time, nil should be strange.


2022-09-30 19:01

This is typo.

amswer→answer


2022-09-30 19:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.