我已经有一个表单可以将SaleQualifiers添加到我的应用中 - 在我使用时可以正常使用:
<%= form_for(@sale_qualifier, :html => {role: :form, 'data-model' => 'sale_qualifier'}, remote: true) do |f| %>
form_for
的问题在于我希望将表单内联在我的视图中的表格行中 - 这是一种解决方法,我现在正在使用:
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
这对于我需要生成的大多数字段都可以正常工作,但我已经为Answer(Answer belongs_to SaleQualifier)提供了一个嵌套属性字段。这不会在视图中生成正确的字段名称,因此当我以这种方式保存对象时,我不会捕获answer_attributes。
使用form_for:
,这是完整的工作表单<div class="panel panel-default">
<%= form_for(@sale_qualifier, :html => {role: :form, 'data-model' => 'sale_qualifier'}, remote: true) do |f| %>
<% if @sale_qualifier.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@sale_qualifier.errors.count, "error") %> prohibited this answer from being saved:</h2>
<ul>
<% @sale_qualifier.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="panel-body">
<div class="col-sm-6">
<h2><%= @question.question_text %></h2>
<% unless @question.id == 1 %>
<p><%= link_to('Back', edit_sale_qualifier_path(id: @prior_sale_qualifier), data: { disable_with: "Loading..." }, :remote => true) %></p>
<% end %>
</div>
<div class="col-sm-6">
<div class="form-group">
<%= f.hidden_field :sales_opportunity_id, :value => @sales_opportunity.id %>
</div>
<div class="form-group">
<%= f.hidden_field :question_id, :value => @question.id %>
</div>
<% unless @question.id == 1 %>
<div class="form-group">
<%= f.hidden_field :prior_question_id, :value => @prior_question_id %>
</div>
<% end %>
<%= f.fields_for :answer do |answer| %>
<div class="form-group">
<% if @question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text, :placeholder => "Enter your answer", :class => "form-control"%>
<% end %>
<% if @question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if @question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<% if @question.answer_type == 'Update' || @question.answer_type == 'Result' %>
<%= answer.hidden_field :answer_text, :value => "Updated" %>
<% end %>
<span class="warning-block"></span>
<span class="help-block"></span>
</div>
<% end %>
<% if @question.answer_type == 'Update' || @question.answer_type == 'Result' %>
<div class="actions">
<%= f.submit "Done", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</div>
<% else %>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
以下是使用form_tag无效的代码:
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.hidden_field :sales_opportunity_id, :value => @sales_opportunity.id %>
<%= ff.hidden_field :question_id, :value => @question.id %>
<tr>
<td><%= @question.question_text %></td>
<td>
<%= ff.fields_for :answer do |answer| %>
<% if @question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text%>
<% end %>
<% if @question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if @question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<% end %>
</td>
<td>
<%= ff.submit "Submit", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</td>
</tr>
<% end %>
<% end %>
为了完整起见,我遇到的问题是为工作代码生成的html会创建以下字段:
textarea id="sale_qualifier_answer_attributes_answer_text" name="sale_qualifier[answer_attributes][answer_text]"
损坏的代码会创建以下html:
Textarea id="sale_qualifier_answer_answer_text" name="sale_qualifier[answer][answer_text]"
那么如何才能显示html输出&#34; sale_qualifier [answer_attributes] [answer_text]&#34;而不是&#34; sale_qualifier [回答] [answer_text]&#34;在这个例子中使用form_tag?
答案 0 :(得分:3)
在多个嵌套表单中,将调用fields_for标记 否则应该遵循正确的父和属性约定 它使表单属性呈现错误并导致错误。
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.fields_for :answer_attributes do |answer| %>
以上流程将是正确的&amp;将生成应有的属性。