我有一个Answer
,其response
属性是一个布尔值。如果我将answer.response
设置为'abcde'
,则响应设置为false
。
class Answer < ActiveRecord::Base
belongs_to :answer_sheet
belongs_to :question
validates :response, inclusion: [true, false]
end
class AnswersController < ApplicationController
def create
answer = Answer.find_or_initialize_by(
answer_sheet_id: answer_params[:answer_sheet_id],
question_id: answer_params[:question_id]
)
answer.response = answer_params[:response]
answer.save
render json: answer, status: :created
end
end
知道为什么会这样吗?我很沮丧。我原以为验证失败了。