简体   繁体   English

Ruby on Rails,一种形式的两个模型

[英]Ruby on Rails, two models in one form

I have two very similar models Pretreatment and Diagnosis, that belong to the model Patient: 我有两个非常相似的模型预处理和诊断,属于模型患者:

class Pretreatment < ActiveRecord::Base
  belongs_to :patient
  attr_accessible :content
end

class Diagnosis < ActiveRecord::Base
  belongs_to :patient
  attr_accessible :content
end

class Patient < ActiveRecord::Base
  attr_accessible :age, :name, :city, :street, :number
  has_many :anamneses
  has_many :befunds
end

On the Patient show page I'm displaying two forms, one for the Preatreatment and another for the Diagnosis : Patient show页面上,我显示两种形式,一种用于Preatreatment ,另一种用于Diagnosis

<%= form_for([@patient, @patient.preatreatments.build]) do |f| %>
  <div class="field">
    <%= f.label :conten %><br />
    <%= f.text_field :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

<%= form_for([@patient, @patient.diagnosiss.build]) do |f| %>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_field :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

My question is how can I bring the two forms together, so that the user only has to press once the submit button? 我的问题是如何将两个表单放在一起,以便用户只需按一次提交按钮? I m not sure but I think nested attributes is not the right thing to handle it, maybe the fields_for` tag? m not sure but I think nested attributes is not the right thing to handle it, maybe the fields_for`标签?

Update I tried to use fields_for tag: 更新我尝试使用fields_for标记:

   <%= form_for([@patient, @patient.pretreatment.build]) do |f| %>
     <div class="field">
       <%= f.label :content %><br />
       <%= f.text_field :content %>
     </div>
     <%= fields_for([@patient, @patient.diagnosiss.build]) do |u| %>
     <div class="field">
       <%= u.label :content %><br />
       <%= u.text_field :content %>
     </div>
     <% end %>
     <div class="actions">
       <%= f.submit %>
     </div>
   <% end %>

But I get the error: 但我得到错误:

undefined method `model_name' for Array:Class in <%= fields_for([@patient,@patient.befunds.build]) do |u| %>

Use fields_for for the associated models. fields_for用于关联模型。
There should be no square brackets arround the parameters of fields_for fields_for的参数fields_for不应有方括号

In your code example, I cannot find the relation between Patient and Diagnosis , and the plural of diagnosis is diagnoses , you can specify this in config/initializers/inflections.rb : 在您的代码示例中,我找不到PatientDiagnosis之间的关系,并且Diagnosis的复数是诊断 ,您可以在config/initializers/inflections.rb指定:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'diagnosis','diagnoses'
end

So your Patient model should contain 所以你的Patient模型应该包含

class Patient < ActiveRecord::Base
  attr_accessible :age, :name, :city, :street, :number
  has_many :diagnoses
end

And you can write in your form: 你可以写下你的表格:

 <div class="field">
   <%= f.label :content %><br />
   <%= f.text_field :content %>
 </div>
 <%= fields_for(@patient, @patient.diagnoses.build) do |u| %>
 <div class="field">
   <%= u.label :content %><br />
   <%= u.text_field :content %>
 </div>
 <% end %>
 <div class="actions">
   <%= f.submit %>
 </div>

You can achieve that using nested attributes : 您可以使用嵌套属性实现此目的:

patient.rb patient.rb

class Patient < ActiveRecord::Base
  attr_accessible :age, :name,  :pretreatments_attributes, :diagnosiss_attributes
  has_many :pretreatments
  has_many :diagnosiss

  accepts_nested_attributes_for :pretreatments
  accepts_nested_attributes_for :diagnosiss
end

patients_controller.rb patients_controller.rb

def show
    @patient = Patient.find(params[:id])
    @patient.pretreatments.build
    @patient.diagnosiss.build
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @patient }
    end
  end

patients/show.html.erb: 患者/ show.html.erb:

<%= form_for @patient do  |f|%>
    <h3>Pretreatments:</h3>

    <%= f.fields_for :pretreatments do |field| %>
        <%= field.label "Content" %></div>
        <%= field.text_field :content %>
    <% end %>

    <h3>Diagnosis:</h3>

    <%= f.fields_for :diagnosiss do |field| %>
        <%= field.label "Content" %></div>
        <%= field.text_field :content %>
    <% end %>

    <%=f.submit  %>
<% end %>

And that all 而这一切

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. fields_for工作方式是你有一个父模型的form_for ,在其中你可以放置fields_for属于父模型的模型。 A good example is given in the Rails Docs Rails文档中给出了一个很好的例子
  2. A simple and more extensible option over time but not very "semantic" one would be to use JavaScript/JQuery. 随着时间的推移,一个简单且可扩展的选项,但不是非常“语义”的选项,将使用JavaScript / JQuery。 You could trigger $("form #new_pretreatments").submit(); 你可以触发$("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. 这可能是最好的选择,而不是为患者可以拥有的每一点信息添加另一个模型。

You can use fields_for for the second model, which works like form_for but doesn't generate the form tags. 您可以将fields_for用于第二个模型,其工作方式类似于form_for但不生成表单标记。 See the docs . 查看文档

There are some gems available for nested forms. 有一些宝石可用于嵌套表单。 one of them is awesome_nested_fields . 其中一个是awesome_nested_fields I haven't used this earlier but that shows good code in documentation. 我之前没有使用它,但在文档中显示了良好的代码。 Another one is simple_form . 另一个是simple_form

Hope that helps!!! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM