将逻辑放入 Rails 应用程序的 Ruby 中:在创建数据库的新行时计算字段的值
Putting logic into a Ruby on Rails application: calculate a value for a field when creating a new row of the database
我对 Rails 上的 Ruby 还很陌生。我正在构建一个 Web 应用程序,它会询问患者一系列问题,然后使用之前经过验证的模型计算风险。创建新患者记录时,我希望应用程序获取用户在其他字段中输入的数据,并使用它来计算最后一个字段的值。我写出了逻辑,但不知道在哪里实际将其编码到我的应用程序中。
它应该放在模型、视图还是控制器中?我应该如何加入它?
我的 git 回购:
https://github.com/Midazolam/anaesthesia
这是我的看法patients/form.html.erb
<%= form_with(model: patient, local: true) do |form| %>
<% if patient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(patient.errors.count, "error") %> prohibited this patient from being saved:</h2>
<ul>
<% patient.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= form.label :"First Name" %><br>
<%= form.text_field :firstName, class: "form-control", id: :patient_firstName %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Surname" %><br>
<%= form.text_field :surName, class: "form-control", id: :patient_surName %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Date of Birth" %>   
<%= form.date_select :dob, :start_year=>1900,:end_year=>2030, id: :patient_dob %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Gender" %>   
<%= form.select :gender, ["","male", "female"], id: :patient_gender %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Which hospital are you having the opperation in?" %>   
<%= form.select :hospital, ["","Beaumont", "Connelly", "St. Josephs", "Naas", "Limerick"], id: :patient_hospital %>
</div>
<hr>
<div class="form-group">
<%= form.label :"What date are you having the opperation on?" %>   
<%= form.date_select :opDate, :start_year=>2017,:end_year=>2027, id: :patient_opDate %>
</div>
<hr>
<div class="form-group">
<%= form.label :"How would you rate your overall health" %><br>
<%= form.select :q1, ["","No abnormalities", "Better than expected", "Worse than expected", "Dementia", "Sick", "Moribund"], id: :patient_q1 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"How is your physical condition in general?" %><br>
<%= form.select :q2, ["","I can do everything I want", "Slightly impaired, but I feel no impairments in daily activities", "Impaired, I can only take care of domestic chores", "I am completely dependent on help from others"], id: :patient_q2 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have a heart condition that impedes your physical efforts (as a result of tiredness, pain or shortness of breath)?" %><br>
<%= form.select :q3, ["","No", "Yes, only during heavy labour", "Yes, during climbing the stairs, in the wind, stress", "Yes, daily, not at rest", "Yes, at rest and increased during any activity"], id: :patient_q3 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever experienced any feeling of pain or tightness in the chest?" %><br>
<%= form.select :q4, ["","No", "Yes, only during heavy labour", "Yes, during climbing the stairs, in the wind, stress", "Yes, daily, not at rest", "Yes, at rest and increased during any activity"], id: :patient_q4 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever had a heart attack?" %><br>
<%= form.select :q5, ["","No", "Yes, > 12 months ago", "Yes, > 6 and < 12 months ago", "Yes, > 3 and < 6 months ago", "Yes, < 3 months ago"], id: :patient_q5 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever had a cardiac arrest?" %><br>
<%= form.select :q6, ["","No", "Yes, > 12 months ago", "Yes, > 6 and < 12 months ago", "Yes, > 3 and < 6 months ago", "Yes, < 3 months ago"], id: :patient_q6 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever undergone any cardiac surgery or heart catheterization?" %><br>
<%= form.select :q7, ["","No", "Yes, > 6 months ago", "Yes, > 3 and < 6 months ago", "Yes, > 6 weeks and < 3 months ago", "Yes, < 6 weeks ago"], id: :patient_q7 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you ever get palpitations?" %><br>
<%= form.select :q8, ["","No", "Yes, I have no treatment", "Yes, I am treated with medication"], id: :patient_q8 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you been told you have a heart murmur?" %><br>
<%= form.select :q9, ["","No", "Yes, but no operation", "Yes, I have had an operation"], id: :patient_q9 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have high blood pressure?" %><br>
<%= form.select :q10, ["","No", "I don't know", "Yes"], id: :patient_q10 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have high cholesterol?" %><br>
<%= form.select :q11, ["","No", "Yes"], id: :patient_q11 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you receive treatment for diabetes (high blood sugar level)?" %><br>
<%= form.select :q12, ["","No", "Yes, treatment with diet", "Yes, treatment with oral medication", "Yes, treatment with insulin"], id: :patient_q12 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever had a stroke?" %><br>
<%= form.select :q13, ["","No", "Yes, but complete recovery", "Yes, with physical impairment/ disability", "Yes, < 2 months ago"], id: :patient_q13 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you receive treatment for asthma?" %><br>
<%= form.select :q14, ["","No", "Yes, sometimes", "Yes, often", "Yes, daily", "Yes, daily and increased over the last six months"], id: :patient_q14 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you receive treatment for pulmonary emphysema, chronic obstructive pulmonary disease (COPD) or chronic bronchitis?" %><br>
<%= form.select :q15, ["","No", "Yes, only during heavy labour", "Yes, always during climbing the stairs, in the wind, stress", "Yes, always even at rest, I need oxygen therapy"], id: :patient_q15 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have Sleep Apnoea?" %><br>
<%= form.select :q16, ["","No", "Yes"], id: :patient_q16 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have any renal problems or kidney impairment?" %><br>
<%= form.select :q17, ["","No", "Yes, but with complete recovery", "Yes, treatment with fluid restriction only", "Yes, treatment with dialysis", "Yes, other serious kidney condition (e.g. kidney resection)"], id: :patient_q17 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever had jaundice or any liver problems?" %><br>
<%= form.select :q18, ["","No", "Yes, but with complete recovery", "Yes, liver cirrhosis has been diagnosed", "Yes, other serious liver condition (e.g. partial resection)"], id: :patient_q18 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever unintentionally lost more than 10 kg in a short period of time?" %><br>
<%= form.select :q19, ["","No", "Yes", "Yes, more than 25% of previous bodyweight"], id: :patient_q19 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you smoke?" %><br>
<%= form.select :q20, ["","No", "Yes"], id: :patient_q20 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you consume alcohol?" %><br>
<%= form.select :q21, ["","No", "Yes, < 3 Units/day", "Yes, > 3 and < 6 Units/day", "Yes, > 6 Units/day"], id: :patient_q21 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you ever take illicit drugs (Cocaine, Heroin, Ecstasy)?" %><br>
<%= form.select :q22, ["","No", "No, not anymore", "Yes, every once in a while", "Yes, more the once a week"], id: :patient_q22 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"ASA grade" %><br>
<%= form.select :asa, ["","1", "2", "3", "4", "5"], id: :patient_asa %>
</div>
<hr>
<div class="form-group">
<%= form.submit "Submit for Assessment", class: "btn btn-primary" %>
</div>
<% end %>
这是我的病人控制器
class PatientsController < ApplicationController
before_action :set_patient, only: [:show, :edit, :update, :destroy]
# GET /patients
# GET /patients.json
def index
@patients = Patient.all
end
# GET /patients/1
# GET /patients/1.json
def show
end
# GET /patients/new
def new
@patient = Patient.new
end
# GET /patients/1/edit
def edit
end
# POST /patients
# POST /patients.json
def create
@patient = Patient.new(patient_params)
respond_to do |format|
if @patient.save
format.html { redirect_to @patient }
format.json { render :show, status: :created, location: @patient }
else
format.html { render :new }
format.json { render json: @patient.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /patients/1
# PATCH/PUT /patients/1.json
def update
respond_to do |format|
if @patient.update(patient_params)
format.html { redirect_to @patient, notice: 'Patient was successfully updated.' }
format.json { render :show, status: :ok, location: @patient }
else
format.html { render :edit }
format.json { render json: @patient.errors, status: :unprocessable_entity }
end
end
end
# DELETE /patients/1
# DELETE /patients/1.json
def destroy
@patient.destroy
respond_to do |format|
format.html { redirect_to patients_url, notice: 'Patient was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_patient
@patient = Patient.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def patient_params
params.require(:patient).permit(:firstName, :surName, :dob, :gender, :hospital, :opDate, :q1, :q2, :q3, :q4, :q5, :q6, :q7, :q8, :q9, :q10, :q11, :q12, :q13, :q14, :q15, :q16, :q17, :q18, :q19, :q20, :q21, :q22, :asa)
end
end
谢谢!
这将进入模型。我可能会将其添加到 before_save
-handler
在您的模型中定义一个方法,其中包含您希望在保存新记录之前 运行 的逻辑。
如果您只想在创建新条目之前 运行 该方法,请使用 :
before_create :your_method
如果您想在每次更新记录时都使用 运行 该方法,请使用:
before_save :your_method
def your_method
self.asa = self.q1 + self.q2 / self.q7
end
如果您使用 before_save 挂钩,每当您尝试保存模型(创建或更新)时,它都会在保存之前将 asa 字段定义为 q1+q2/q7,这将导致字段 .asa 正在填充。
我对 Rails 上的 Ruby 还很陌生。我正在构建一个 Web 应用程序,它会询问患者一系列问题,然后使用之前经过验证的模型计算风险。创建新患者记录时,我希望应用程序获取用户在其他字段中输入的数据,并使用它来计算最后一个字段的值。我写出了逻辑,但不知道在哪里实际将其编码到我的应用程序中。
它应该放在模型、视图还是控制器中?我应该如何加入它?
我的 git 回购: https://github.com/Midazolam/anaesthesia
这是我的看法patients/form.html.erb
<%= form_with(model: patient, local: true) do |form| %>
<% if patient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(patient.errors.count, "error") %> prohibited this patient from being saved:</h2>
<ul>
<% patient.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= form.label :"First Name" %><br>
<%= form.text_field :firstName, class: "form-control", id: :patient_firstName %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Surname" %><br>
<%= form.text_field :surName, class: "form-control", id: :patient_surName %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Date of Birth" %>   
<%= form.date_select :dob, :start_year=>1900,:end_year=>2030, id: :patient_dob %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Gender" %>   
<%= form.select :gender, ["","male", "female"], id: :patient_gender %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Which hospital are you having the opperation in?" %>   
<%= form.select :hospital, ["","Beaumont", "Connelly", "St. Josephs", "Naas", "Limerick"], id: :patient_hospital %>
</div>
<hr>
<div class="form-group">
<%= form.label :"What date are you having the opperation on?" %>   
<%= form.date_select :opDate, :start_year=>2017,:end_year=>2027, id: :patient_opDate %>
</div>
<hr>
<div class="form-group">
<%= form.label :"How would you rate your overall health" %><br>
<%= form.select :q1, ["","No abnormalities", "Better than expected", "Worse than expected", "Dementia", "Sick", "Moribund"], id: :patient_q1 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"How is your physical condition in general?" %><br>
<%= form.select :q2, ["","I can do everything I want", "Slightly impaired, but I feel no impairments in daily activities", "Impaired, I can only take care of domestic chores", "I am completely dependent on help from others"], id: :patient_q2 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have a heart condition that impedes your physical efforts (as a result of tiredness, pain or shortness of breath)?" %><br>
<%= form.select :q3, ["","No", "Yes, only during heavy labour", "Yes, during climbing the stairs, in the wind, stress", "Yes, daily, not at rest", "Yes, at rest and increased during any activity"], id: :patient_q3 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever experienced any feeling of pain or tightness in the chest?" %><br>
<%= form.select :q4, ["","No", "Yes, only during heavy labour", "Yes, during climbing the stairs, in the wind, stress", "Yes, daily, not at rest", "Yes, at rest and increased during any activity"], id: :patient_q4 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever had a heart attack?" %><br>
<%= form.select :q5, ["","No", "Yes, > 12 months ago", "Yes, > 6 and < 12 months ago", "Yes, > 3 and < 6 months ago", "Yes, < 3 months ago"], id: :patient_q5 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever had a cardiac arrest?" %><br>
<%= form.select :q6, ["","No", "Yes, > 12 months ago", "Yes, > 6 and < 12 months ago", "Yes, > 3 and < 6 months ago", "Yes, < 3 months ago"], id: :patient_q6 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever undergone any cardiac surgery or heart catheterization?" %><br>
<%= form.select :q7, ["","No", "Yes, > 6 months ago", "Yes, > 3 and < 6 months ago", "Yes, > 6 weeks and < 3 months ago", "Yes, < 6 weeks ago"], id: :patient_q7 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you ever get palpitations?" %><br>
<%= form.select :q8, ["","No", "Yes, I have no treatment", "Yes, I am treated with medication"], id: :patient_q8 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you been told you have a heart murmur?" %><br>
<%= form.select :q9, ["","No", "Yes, but no operation", "Yes, I have had an operation"], id: :patient_q9 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have high blood pressure?" %><br>
<%= form.select :q10, ["","No", "I don't know", "Yes"], id: :patient_q10 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have high cholesterol?" %><br>
<%= form.select :q11, ["","No", "Yes"], id: :patient_q11 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you receive treatment for diabetes (high blood sugar level)?" %><br>
<%= form.select :q12, ["","No", "Yes, treatment with diet", "Yes, treatment with oral medication", "Yes, treatment with insulin"], id: :patient_q12 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever had a stroke?" %><br>
<%= form.select :q13, ["","No", "Yes, but complete recovery", "Yes, with physical impairment/ disability", "Yes, < 2 months ago"], id: :patient_q13 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you receive treatment for asthma?" %><br>
<%= form.select :q14, ["","No", "Yes, sometimes", "Yes, often", "Yes, daily", "Yes, daily and increased over the last six months"], id: :patient_q14 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you receive treatment for pulmonary emphysema, chronic obstructive pulmonary disease (COPD) or chronic bronchitis?" %><br>
<%= form.select :q15, ["","No", "Yes, only during heavy labour", "Yes, always during climbing the stairs, in the wind, stress", "Yes, always even at rest, I need oxygen therapy"], id: :patient_q15 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have Sleep Apnoea?" %><br>
<%= form.select :q16, ["","No", "Yes"], id: :patient_q16 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you have any renal problems or kidney impairment?" %><br>
<%= form.select :q17, ["","No", "Yes, but with complete recovery", "Yes, treatment with fluid restriction only", "Yes, treatment with dialysis", "Yes, other serious kidney condition (e.g. kidney resection)"], id: :patient_q17 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever had jaundice or any liver problems?" %><br>
<%= form.select :q18, ["","No", "Yes, but with complete recovery", "Yes, liver cirrhosis has been diagnosed", "Yes, other serious liver condition (e.g. partial resection)"], id: :patient_q18 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Have you ever unintentionally lost more than 10 kg in a short period of time?" %><br>
<%= form.select :q19, ["","No", "Yes", "Yes, more than 25% of previous bodyweight"], id: :patient_q19 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you smoke?" %><br>
<%= form.select :q20, ["","No", "Yes"], id: :patient_q20 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you consume alcohol?" %><br>
<%= form.select :q21, ["","No", "Yes, < 3 Units/day", "Yes, > 3 and < 6 Units/day", "Yes, > 6 Units/day"], id: :patient_q21 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"Do you ever take illicit drugs (Cocaine, Heroin, Ecstasy)?" %><br>
<%= form.select :q22, ["","No", "No, not anymore", "Yes, every once in a while", "Yes, more the once a week"], id: :patient_q22 %>
</div>
<hr>
<div class="form-group">
<%= form.label :"ASA grade" %><br>
<%= form.select :asa, ["","1", "2", "3", "4", "5"], id: :patient_asa %>
</div>
<hr>
<div class="form-group">
<%= form.submit "Submit for Assessment", class: "btn btn-primary" %>
</div>
<% end %>
这是我的病人控制器
class PatientsController < ApplicationController
before_action :set_patient, only: [:show, :edit, :update, :destroy]
# GET /patients
# GET /patients.json
def index
@patients = Patient.all
end
# GET /patients/1
# GET /patients/1.json
def show
end
# GET /patients/new
def new
@patient = Patient.new
end
# GET /patients/1/edit
def edit
end
# POST /patients
# POST /patients.json
def create
@patient = Patient.new(patient_params)
respond_to do |format|
if @patient.save
format.html { redirect_to @patient }
format.json { render :show, status: :created, location: @patient }
else
format.html { render :new }
format.json { render json: @patient.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /patients/1
# PATCH/PUT /patients/1.json
def update
respond_to do |format|
if @patient.update(patient_params)
format.html { redirect_to @patient, notice: 'Patient was successfully updated.' }
format.json { render :show, status: :ok, location: @patient }
else
format.html { render :edit }
format.json { render json: @patient.errors, status: :unprocessable_entity }
end
end
end
# DELETE /patients/1
# DELETE /patients/1.json
def destroy
@patient.destroy
respond_to do |format|
format.html { redirect_to patients_url, notice: 'Patient was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_patient
@patient = Patient.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def patient_params
params.require(:patient).permit(:firstName, :surName, :dob, :gender, :hospital, :opDate, :q1, :q2, :q3, :q4, :q5, :q6, :q7, :q8, :q9, :q10, :q11, :q12, :q13, :q14, :q15, :q16, :q17, :q18, :q19, :q20, :q21, :q22, :asa)
end
end
谢谢!
这将进入模型。我可能会将其添加到 before_save
-handler
在您的模型中定义一个方法,其中包含您希望在保存新记录之前 运行 的逻辑。
如果您只想在创建新条目之前 运行 该方法,请使用 :
before_create :your_method
如果您想在每次更新记录时都使用 运行 该方法,请使用:
before_save :your_method
def your_method
self.asa = self.q1 + self.q2 / self.q7
end
如果您使用 before_save 挂钩,每当您尝试保存模型(创建或更新)时,它都会在保存之前将 asa 字段定义为 q1+q2/q7,这将导致字段 .asa 正在填充。