我正在使用ruby on rails上的Q& A应用程序。 我收到了错误,因为nil无法被强制进入Fixnum"这个应用程序在heroku上工作正常(请参阅底部的演示项目链接。)
这是我的错误:
nil can't be coerced into Fixnum
Extracted source (around line #28):
#26 - maximum_score = 0
#27 - survey.questions.each do |question|
#28 - maximum_score += question.answers.map(&:score).compact.max
#29 - new_filled_question = filled_questions.build :content => question.content
#30 - question.answers.each do |answer|
#31 - new_filled_question.filled_answers.build :content => answer.content, :score => answer.score
app/models/filled_survey.rb:28:in `+'
app/models/filled_survey.rb:28:in `block in custom_init'
app/models/filled_survey.rb:27:in `custom_init'
app/controllers/surveys_controller.rb:47:in `fill'
我已经工作了" + ="但仍然没用。 这是我的app / models / filled_survey.rb:
class FilledSurvey < ActiveRecord::Base
has_many :filled_questions, :dependent => :destroy, autosave: true
belongs_to :survey
accepts_nested_attributes_for :filled_questions
after_initialize :custom_init
validates_associated :filled_questions
scope :completed, -> { where(completed: true) }
scope :uncompleted, -> { where(completed: nil) }
def total_score
sum = 0
filled_questions.each do |filled_question|
filled_answer = filled_question.filled_answers.find_by_id( filled_question.filled_choice.selected)
if filled_answer
sum += filled_answer.score
end
end
sum
end
def custom_init
# save those values since they could change
self.name ||= survey.name
if filled_questions.empty?
maximum_score = 0
survey.questions.each do |question|
maximum_score += question.answers.map(&:score).max
new_filled_question = filled_questions.build :content => question.content
question.answers.each do |answer|
new_filled_question.filled_answers.build :content => answer.content, :score => answer.score
new_filled_question.build_filled_choice
end
end
self.maximum_score = maximum_score
end
end
end
这是我的app / controllers / surveys_controller.rb
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy, :fill]
def index
@surveys = Survey.all
end
def show
end
def new
@survey = Survey.new
end
def edit
end
def create
@survey = Survey.new(survey_params)
if @survey.save
redirect_to @survey, notice: 'Survey was successfully created.'
else
render :new
end
end
def update
if @survey.update(survey_params)
redirect_to @survey, notice: 'Survey was successfully updated.'
else
render :edit
end
end
def destroy
@survey.destroy
redirect_to surveys_url, notice: 'Survey was successfully destroyed.'
end
def fill
# TODO check if current user has to fill tha last version, or some older
if @survey.filled_surveys.empty?
filled_survey = @survey.filled_surveys.create
elsif @survey.filled_surveys.uncompleted.empty?
# all are completed
filled_survey = @survey.filled_surveys.create
else
# we have some uncompleted
filled_survey = @survey.filled_surveys.uncompleted.first
end
redirect_to edit_filled_survey_path(filled_survey)
end
private
def set_survey
@survey = Survey.find(params[:id])
end
def survey_params
params.require(:survey).permit(:name, questions_attributes: [:id, :content, :_destroy, answers_attributes: [:id, :score, :content, :_destroy]] )
end
end
github和herokuapp demo上的示例应用