ruby-on-rails - 合并两个表单 rails

标签 ruby-on-rails ruby-on-rails-3

基本上我在这里遇到了与 @user1224344 相同的问题:

How to submit multiple, duplicate forms from same page in Rails - preferably with one button

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

<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>

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

最佳答案

你已经很接近了。

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

patient.rb

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

治疗.rb

belongs_to :patient

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

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

因此,您已经为患者 提交了一份表格,该表格提交了两种治疗表格,自动与患者相关联。我可能搞砸了那里的一些细节,但这是基本的想法。

编辑——您可能需要查看 this out .最好像他们在那个问题中所做的那样,将处理表单对象的构建放在 Controller 中。你可能想查看 Rails API for more specific help on accepts_nested_attributes_for.

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

如果您在 Controller 中构建表单对象 --

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

-- 然后你可以在你的 View 中这样做:

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

此外,如果不清楚,根据您上面的代码,您的部分内容应该是这样的:

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

局部 View

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

换句话说,您可以将对 fields_for 的调用移到局部函数中,这可能更有意义。实际上不应该改变任何东西的实际运作方式。

关于ruby-on-rails - 合并两个表单 rails ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17775118/

相关文章:

ruby-on-rails - 使用 Heroku 和 CloudFront 从 RapidSSL 获得的 SSL 证书

ruby-on-rails - 使用Elasticsearch进行Rails预测性搜索表单建议

ruby-on-rails - rails-4 没有通过连接从 2 个表中获取所有列

ruby-on-rails - 使用工具/插件/脚本可视化 Rails schema.rb

ruby-on-rails - Heroku 不更新 Assets 修改

sql - Rails 在 ActiveRecord::Relation 中使用 OR 进行搜索

jquery - Rails 3 ajax 调用 - ActionView::MissingTemplate

ruby-on-rails - 寻找RSpec的GUI

ruby-on-rails - ruby on Rails 教程 TDD "Email has already been taken"

ruby-on-rails-3 - 我可以将这个 Rails 计算插入数据库吗?