【问题标题】:Push value to model when created - Rails创建时将值推送到模型 - Rails
【发布时间】: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: '医疗团队')

在控制台中返回

#&lt;Inputter {"id"=&gt;309, "inputter_type"=&gt;"Medical team"}&gt;

任何关于如何获得价值以坚持新 EsasAssessment 的建议都会很棒

【问题讨论】:

  • 不完全是主题,但在创建操作中使用您的esas_assessment_params = params.require(:esas_assessment).permit(:patient_id,... 似乎很奇怪并且可能不安全。它通常在一个私有方法中。
  • 另外,你能在 Rails 控制台中重新创建它吗?
  • 为了简化,我只是将其移出私有。请参阅有关在控制台中尝试的问题的底部

标签: ruby-on-rails ruby forms model controller


【解决方案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')

【讨论】:

    猜你喜欢
    • 2016-10-29
    • 2016-10-15
    • 2021-12-20
    • 2018-07-18
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多