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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17131395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:10:03  来源:igfitidea点击:

Ruby on Rails, two models in one form

ruby-on-railsrubyruby-on-rails-3

提问by Em Sta

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 Patientshow page I'm displaying two forms, one for the Preatreatmentand another for the Diagnosis:

Patient显示页面上,我显示了两种形式,一种用于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? Im not sure but I think nested attributes is not the right thing to handle it, maybe thefields_for` tag?

我的问题是如何将两个表单放在一起,以便用户只需按一次提交按钮?我m not sure but I think nested attributes is not the right thing to handle it, maybe thefields_for` 标签?

UpdateI tried to use fields_fortag:

更新我尝试使用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| %>

回答by Martin M

Use fields_forfor the associated models.
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 Patientand Diagnosis, and the plural of diagnosis is diagnoses, you can specify this in config/initializers/inflections.rb:

在你的代码示例中,我找不到之间的关系PatientDiagnosis诊断的复数形式,和诊断,可以在指定此config/initializers/inflections.rb

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

So your Patientmodel 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>

回答by Muntasim

You can achieve that using nested attributes :

您可以使用嵌套属性来实现:

patient.rb

病人.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

患者控制器.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

而这一切

回答by roryhughes

There are a few ways of doing this:

有几种方法可以做到这一点:

  1. The way the fields_forworks is you have a form_forfor the parent model and within it you can place fields_forthe 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.
  1. fields_for工作方式是你有一个form_for父模型,在它里面你可以放置fields_for属于父模型的模型。Rails Docs 中给出了一个很好的例子
  2. 随着时间的推移,一个简单且更具扩展性但不是很“语义”的选项是使用 JavaScript/JQuery。$("form #new_pretreatments").submit();单击按钮后,您可以触发和相同的诊断。
  3. 也许你可以只用一个模型来存储它们。例如一个名为疾病的模型。每次患者患上疾病时都会创建一个新疾病,并且每个诊断和预处理都有列。这可能是最好的选择,而不是为患者可以获得的每一位信息添加另一个模型。

回答by Sven Koschnicke

You can use fields_forfor the second model, which works like form_forbut doesn't generate the form tags. See the docs.

您可以使用fields_for第二个模型,它的工作原理类似于form_for但不生成表单标签。请参阅文档

回答by Anand Soni

There are some gems available for nested forms. one of them is awesome_nested_fields. I haven't used this earlier but that shows good code in documentation. Another one is simple_form.

有一些 gems 可用于嵌套表单。其中之一是awesome_nested_fields。我之前没有使用过这个,但在文档中显示了很好的代码。另一个是simple_form

Hope that helps!!!

希望有帮助!!!