合并两种形式的导轨 [英] Merge two forms rails

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

问题描述

基本上我在这里遇到与@user1224344 一样的问题:

Basiclly i have the same issue like @user1224344 here:

如何提交Rails 中来自同一页面的多个重复表单 - 最好使用一个按钮

第一个答案看起来很不错,但是作为 Rails 初学者,在我的项目中将其转置时遇到了问题.好的,我有两个具有相同控制器的表单,应该只保存一个提交按钮:

And the first answer looks quite nice, but i as rails beginner have problems to transpose it in my project. Ok i have two forms with the same controller that should be saved with only one submit button:

<table width="100%">
<tr>
<%= form_for([@patient, @patient.treatments.build]) do |f| %>
<th><%= f.collection_select :category_id, Category.find(:all), :id, :typ %></th>
<th><%= f.text_field :content %></th>
<th><%= f.hidden_field :note, :id => "canvascontent" %></th>
<th><%= f.text_field :day, :value => Date.today %></th>
<th><%= f.submit  :class => 'btn btn-small btn-primary', :onclick => "sketch.toDataURL()"  %></th>
<th><input type="button" onclick="sketch.clearRecording()" class="btn btn-small btn-danger" value="Löschen"></th>
<% end %>
</tr>
</table>


<table width="100%">
<tr>
<%= form_for([@patient, @patient.treatments.build]) do |f| %>
<th><%= f.collection_select :category_id, Category.find(:all), :id, :typ %></th>
<th><%= f.text_field :content , :id => "inputbox"%></th>
<th><%= f.text_field :day, :value => Date.today %></th>
<th><%= f.submit  :class => 'btn btn-small btn-primary'%></th>
<% end %>
</tr>
</table>

感谢帮助!特别是在周日晚上(至少在德国)

Thanks for help! Espacially on sunday night(at least here in germany)

推荐答案

你们已经很接近了.

诀窍是两个表单都应该嵌套在提交它们的表单中,这应该是另一个模型.我不知道您的应用程序是如何组合在一起的,但我会假设患者有很多治疗.您的模型应包含以下内容:

The trick is that both forms should be nested within the form that submits them, which should be another model. I don't know how your app is put together, but I'll assume that Patients have many Treatments. Your models should include this:

patient.rb

attr_accessible :treatments_attributes, etc...
has_many :treatments
accepts_nested_attributes_for :treatments

treatment.rb

belongs_to :patient

如您所见,患者接受其治疗的属性(因此是该模型中的第三行和第一行).因此,您实际上需要将治疗表格包装在患者表格中,以便您提交带有嵌套治疗的患者表格.像这样:

As you can see, patient accepts the attributes for its treatments (thus the third and first line in that model). So you actually need to wrap the treatment forms in a patient form, so that you're submitting the patient form with its nested treatments. Something like this:

<%= form_for @patient do |f| %>
  <%= f.fields_for @patient.build_treatment do |tf| %>
    <%= render 'treatment_form', locals: { form: tf } %>
  <% end %>
  <%= f.fields_for @patient.build_treatment do |tf| %>
    <%= render 'treatment_form', locals: { form: tf } %>
  <% end %>
  <%= f.submit %>
<% end %>

因此,您为患者准备了一份表单,该表单同时提交了两份治疗表单,自动与患者关联.我可能搞砸了那里的一些细节,但这是基本思想.

So you've got that one form for the patient that's submitting both treatment forms, automatically associated with the patient. I may have messed up some of the specifics there, but that's the basic idea.

编辑 -- 您可能需要检查 这个输出.最好将处理表单对象的构建放在您的控制器中,就像他们在该问题中所做的那样.您可能想查看 Rails API 以获取有关 accepts_nested_attributes_for 的更具体帮助.

EDIT -- you may want to check this out. It's best to put the building of the treatment form objects in your controller like they do in that question. And you may want to check out the Rails API for more specific help on accepts_nested_attributes_for.

此外,如果不清楚,locals"只是将处理表单对象传递给变量名称form"下的部分,因此在该部分中,您可以编写 <%= form.label :无论 %>... 等等,在那个部分中.

Also, in case it's unclear, the "locals" thing is just passing the treatment form object to a partial under the variable name "form", so in that partial, you'd write <%= form.label :whatever %>... etc, within that partial.

如果您在控制器中构建表单对象 --

If you build out the form objects in your controller --

@patient.build_treatments #may not be exactly this, but it's close

-- 然后您可以在您的视图中执行此操作:

-- then you can do this in your view:

<%= f.fields_for :treatment do |tf| %>

另外,如果不清楚,根据上面的代码,你的部分看起来像这样:

Also, if it's not clear, your partial would look something like this, based on your above code:

<table width="100%">
  <tr>
    <th><%= form.collection_select :category_id, Category.find(:all), :id, :typ %></th>
    <th><%= form.text_field :content , :id => "inputbox"%></th>
    <th><%= form.text_field :day, :value => Date.today %></th>
  </tr>
</table>

其他可能更直观的布局:

主视图

<%= form_for @patient do |f| %>
  <%= render 'treatment_form', form: f %>
<% end %>

部分视图

<%= form.fields_for :treatment do |field| %>
  <% field.label :whatever %> #...

换句话说,您可以将调用移至部分内部的 fields_for,这可能更有意义.实际上不应该改变任何事情的实际运作方式.

In other words, you'd move the call to fields_for inside the partial, which might make more sense. Shouldn't actually change how anything really works.

这篇关于合并两种形式的导轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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