[英]NoMethodError (undefined method `each' for nil:NilClass):
class Api::SurveyAnswersController < ApplicationController
def create
# @survey_answer = SurveyAnswer.new(survey_answer_params)
survey_answers = []
survey_id = params[:survey_id]
params[:questions].each do |q|
answer = {survey_id: survey_id, option_ids: [], question_id: q[:id],
title: q[:answer]}
if q[:options].present?
selected_options = q[:answer].split(',')
selected_options.each do |selected_option|
q[:options].each do |option|
if option[:title]== selected_option
answer[:option_ids] << option[:id]
#<todo add break when in this condition
end
end
end
survey_answers << answer
end
end
puts survey_answers
# @survey_answers = SurveyAnswer.create(survey_answers)
if SurveyAnswer.create(survey_answers)
render json: survey_answers
end
end
end
我有一個調查模型,其中有一些問題。 每個問題都包含答案。 當我嘗試通過郵遞員點擊發布請求以插入答案時,它給出了 505 內部服務器錯誤,並帶有消息“每個 nil:nilclass 的未定義方法”。 任何人都可以告訴問題是什么?
您正在嘗試運行.each循環一個空對象。
確保兩者
params[:questions]
和
q[:options]
不為空(不等於nil )。
NoMethodError有時聽起來很沒有代表性,尤其是當您剛開始使用 Ruby 時。
下次嘗試瀏覽Stackoverflow,因為這里已經回答了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.