Ruby on Rails, two models in one form

后端 未结 5 1152
我在风中等你
我在风中等你 2021-01-31 05:57

I have two very similar models Pretreatment and Diagnosis, that belong to the model Patient:

class Pretreatment < ActiveRecord::Base
  belongs_to :patient
  a         


        
5条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 06:23

    There are a few ways of doing this:

    1. The way the fields_for works is you have a form_for for the parent model and within it you can place fields_for the models which belong to the parent. A good example is given in the Rails Docs
    2. A simple and more extensible option over time but not very "semantic" one would be to use JavaScript/JQuery. You could trigger $("form #new_pretreatments").submit(); and the same for the Diagnosis once a button is clicked.
    3. Maybe instead you could have just one model to store them both. For example a model called Disease. Each time a patient gets a disease a new one is created and it has columns for each the Diagnosis and Pretreatment. It's probably the best option instead of adding another model for each bit of info a patient can have.

提交回复
热议问题