17

我有两个非常相似的模型预处理和诊断,属于模型患者:

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

Patient显示页面上,我显示了两种形式,一种用于 the 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 %>

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

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

但我得到了错误:

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

5 回答 5

20

用于fields_for关联模型。
参数周围不应有方括号fields_for

在您的代码示例中,我找不到 and 之间的关系PatientDiagnosis并且 diagnostic 的复数是diagnostics,您可以在以下位置指定config/initializers/inflections.rb

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

所以你的Patient模型应该包含

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

你可以用你的表格写:

 <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>
于 2013-06-16T09:19:16.570 回答
8

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

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

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

患者/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 %>

而这一切

于 2013-06-16T09:26:46.747 回答
2

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

  1. 作品的方式fields_for是你有一个form_for父模型,你可以fields_for在其中放置属于父模型的模型。Rails Docs中给出了一个很好的例子
  2. 随着时间的推移,一个简单且更可扩展但不是很“语义”的选项是使用 JavaScript/JQuery。$("form #new_pretreatments").submit();单击按钮后,您可以触发和相同的诊断。
  3. 也许相反,您可以只使用一个模型来存储它们。例如一个名为疾病的模型。每次患者患上一种疾病时,都会创建一个新的疾病,并且每个诊断和预处理都有列。这可能是最好的选择,而不是为患者可以拥有的每一位信息添加另一个模型。
于 2013-06-16T09:37:12.850 回答
1

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

于 2013-06-16T08:20:29.357 回答
1

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

希望有帮助!!!

于 2013-06-16T08:25:40.413 回答