【发布时间】:2015-05-06 04:53:41
【问题描述】:
如果用户是current_clinician,我正在尝试将inputter_id 的值自动设置为id 对应于“医疗团队”的表单中的id:
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 的建议都会很棒
【问题讨论】:
-
不完全是主题,但在创建操作中使用您的
esas_assessment_params = params.require(:esas_assessment).permit(:patient_id,...似乎很奇怪并且可能不安全。它通常在一个私有方法中。 -
另外,你能在 Rails 控制台中重新创建它吗?
-
为了简化,我只是将其移出私有。请参阅有关在控制台中尝试的问题的底部
标签: ruby-on-rails ruby forms model controller