0

我对Ruby on Rails相当陌生。我正在构建一个Web应用程序,向患者询问一系列问题,然后使用先前验证的模型计算风险。当创建新的患者记录时,我希望应用程序将用户输入的数据输入到其他字段中,并使用它计算最后一个字段的值。我已经写出逻辑,但只是不知道在哪里实际编码到我的应用程序。将逻辑放入Ruby on Rails应用程序中:在创建数据库的新行时计算字段的值

它应该在模型,视图或控制器?我应该怎么去包括它?

我的混帐回购协议: https://github.com/Midazolam/anaesthesia

这是我的看法患者/ 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" %>&nbsp &nbsp 
 
    <%= form.date_select :dob, :start_year=>1900,:end_year=>2030, id: :patient_dob %> 
 
    </div> 
 
    <hr> 
 

 
    <div class="form-group"> 
 
    <%= form.label :"Gender" %>&nbsp &nbsp 
 
    <%= form.select :gender, ["","male", "female"], id: :patient_gender %> 
 
    </div> 
 
    <hr> 
 

 
    <div class="form-group"> 
 
    <%= form.label :"Which hospital are you having the opperation in?" %>&nbsp &nbsp 
 
    <%= 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?" %>&nbsp &nbsp 
 
    <%= 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

谢谢!

回答

1

在您的模型中定义一个方法,其中包含您希望在保存新记录之前运行的逻辑。

如果你只想要创建一个新的条目之前运行的方法,使用方法:

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。

+0

非常感谢! –

0

这将进入模型。我可能会将它添加到before_save -handler

+0

非常感谢! –

相关问题