如果用户是java.util.Currency
,我正尝试将inputter_id
值自动设置为id
对应于表单中“医疗团队”的inputter_type
:
current_clinician
或更简单:
def create
esas_assessment_params = params.require(:esas_assessment).permit(:patient_id, :clinician_id, :time, :year, :month, :day, :inputter_name, :inputter_id, :pain, :pain_comment, :tiredness, :tiredness_comment, :drowsiness, :drowsiness_comment, :nausea, :nausea_comment, :lack_of_appetite, :lack_of_appetite_comment, :shortness_of_breath, :shortness_of_breath_comment, :depression, :depression_comment, :wellbeing, :wellbeing_comment, :other_symptom_id, :other_symptom_score, :other_symptom_comment, :esas_comment)
@esas_assessment = EsasAssessment.new(esas_assessment_params)
if current_clinician
@esas_assessment.clinician = current_user.clinician
@esas_assessment.inputter_name = current_user.clinician.full_name
@esas_assessment.inputter_id = Inputter.find_by(inputter_type: 'Medical team')
else
@esas_assessment.patient = current_user.patient
@esas_assessment.clinician = current_user.patient.clinician
end
if @esas_assessment.save
redirect_to esas_assessments_path, notice: "ESAS assessment submitted!"
else
render "new", alert: "ESAS assessment not submitted!"
end
end
def create esas_assessment_params = params.require(:esas_assessment).permit! @esas_assessment = EsasAssessment.new(esas_assessment_params)
@esas_assessment.inputter_id = Inputter.find_by(inputter_type: 'Medical team')
在第一个示例中,将 if @esas_assessment.save
redirect_to esas_assessments_path, notice: "ESAS assessment submitted!"
else
render "new", alert: "ESAS assessment not submitted!"
end
end
值自动设置为@esas_assessment.clinician
并将current_user.clinician
设置为“inputter_name
但inputter_id
不起作用的行。
我的EsasAssessment模型是:
EsasAssessment:
patient_id: integer
clinician_id: integer
created_at: datetime
updated_at: datetime
inputter_name: string
inputter_id: integer
class EsasAssessment < ActiveRecord::Base
belongs_to :other_symptom
belongs_to :clinician
belongs_to :patient
belongs_to :inputter
end
输入器是:
Inputter:
inputter_type: string
class Inputter < ActiveRecord::Base
has_many :esas_assessments
end
当我提交表单时,我没有收到任何错误或警告,inputter_id
只是nil
如果我输入
在控制台中Inputter.find_by(inputter_type:'医疗团队')
返回
#<Inputter {"id"=>309, "inputter_type"=>"Medical team"}>
关于如何使价值坚持新的EsasAssessment的任何建议都会很棒
答案 0 :(得分:1)
猜猜你应该分配找到的输入器的id
,而不是该行中的对象本身
@esas_assessment.inputter_id = Inputter.find_by(inputter_type: 'Medical team')
像
这样的东西@esas_assessment.inputter_id = Inputter.find_by(inputter_type: 'Medical team').try(:id)
或者
@esas_assessment.inputter = Inputter.find_by(inputter_type: 'Medical team')