Ruby on Rails,一种形式的两种模型 [英] Ruby on Rails, two models in one form

查看:36
本文介绍了Ruby on Rails,一种形式的两种模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个非常相似的模型 Pretreatment 和 Diagnosis,属于模型 Patient:

类预处理

Patient 显示页面上,我显示两种形式,一种用于 Preatreatment,另一种用于 Diagnosis:

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

<div class="actions"><%= f.submit %>

<%结束%><%= form_for([@patient, @patient.diagnosiss.build]) 做 |f|%><div class="field"><%= f.label :content%><br/><%= f.text_field :content %>

<div class="actions"><%= f.submit %>

<%结束%>

我的问题是如何将两个表单放在一起,以便用户只需按一次提交按钮?我不确定,但我认为嵌套属性不是处理它的正确方法,也许是fields_for`标签?

更新我尝试使用 fields_for 标签:

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

<%= fields_for([@patient, @patient.diagnosiss.build]) 做 |u|%><div class="field"><%=u.label:content%><br/><%= u.text_field :content %>

<%结束%><div class="actions"><%= f.submit %>

<%结束%>

但我收到错误:

未定义方法`model_name' for Array:Class in <%= fields_for([@patient,@patient.befunds.build]) do |u|%>

解决方案

对关联模型使用 fields_for.
fields_for

的参数周围不应有方括号

在您的代码示例中,我找不到PatientDiagnosis 之间的关系,并且诊断的复数是diagnoses,您可以指定这在 config/initializers/inflections.rb 中:

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

所以你的 Patient 模型应该包含

class 患者 

你可以在你的表单中写:

 

<%= f.label :content%><br/><%= f.text_field :content %>

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

<%结束%><div class="actions"><%= f.submit %>

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:

<%= 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?

Update I tried to use fields_for tag:

   <%= 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.
There should be no square brackets arround the parameters of 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:

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

So your Patient model should contain

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>

这篇关于Ruby on Rails,一种形式的两种模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆