[英]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
: 在您的代码示例中,我找不到Patient
和Diagnosis
之间的关系,并且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: 有几种方法可以做到这一点:
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文档中给出了一个很好的例子 $("form #new_pretreatments").submit();
你可以触发$("form #new_pretreatments").submit();
and the same for the Diagnosis once a button is clicked. 单击一个按钮,对于诊断也是如此。 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.