我正在尝试在有多个复选框的rails中发布一个表单,但是我在过去几天遇到了麻烦。下面是带有复选框的表单
<%=form_tag (diffreport_path) do |f|%>
<div>
<table class="table table-bordered table-striped" id="Diff_table">
<thead>
<th>File name</th>
<th>Download link</th>
<th>Select</th>
</thead>
<tbody>
<% @files.each do |file|%>
<tr>
<td><%= file.name%></td>
<td><%= link_to "Download_file", file.attachment_url%></td>
<td><%= check_box_tag "file_ids[]", file.id %> </td>
</tr>
<% end %>
</tbody>
</table>
<br>
</div>
<div class="col-md-4 col-md-offset-4" id="diff_report_div">
<button class="btn btn-primary" id="clear_selection">Clear Selection</button>
<br>
<br>
<%= submit_tag "Create a Diff report",class: "btn btn-primary"%>
</div>
<% end %>
当我检查一个方框时,我可以传递params hash
{"utf8"=>"✓",
"authenticity_token"=>"cY1ugfVkbkg3gILiJEtJo5L5LaQjG9R2jj5RRTe11n4=",
"file_ids"=>["9"],
"commit"=>"Create a Diff report"
}
但是当我检查多个复选框时。我没有看到在params散列中传递任何值。
{"utf8"=>"✓",
"authenticity_token"=>"cY1ugfVkbkg3gILiJEtJo5L5LaQjG9R2jj5RRTe11n4=",
"commit"=>"Create a Diff report"
}
当我检查HTML源代码时。它似乎很好,但我可能会遗漏一些东西。在呈现的HTML代码下面。我不确定为什么会这样。
<form accept-charset="UTF-8" action="/diffreport" method="post">
<div style="display:none">
<input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="cY1ugfVkbkg3gILiJEtJo5L5LaQjG9R2jj5RRTe11n4=" />
</div>
<div>
<table class="table table-bordered table-striped" id="Diff_table">
<thead>
<th>File name</th>
<th>Download link</th>
<th>Select</th>
</thead>
<tbody>
<tr>
<td>RDK_v8</td>
<td>
<a href="/uploads/data_model_file/attachment/9/RDKVB_DataModel_v2.9.xlsm">
Download_fi le</a>
</td>
<td><input checked="checked" id="file_names_" name="file_names[]" type= "checkbox" value="9" />
</td>
</tr>
<tr>
<td>RDK_v10</td>
<td><a href="/uploads/data_model_file/attachment/10/RDKVB_DataModel_v2.9.xlsm">Download_f ile</a></td>
<td><input checked="checked" id="file_names_" name="file_names[]" type="checkbox" value="10" /> </td>
</tr>
<tr>
<td>RDK_v11</td>
<td><a href="/uploads/data_model_file/attachment/11/RDKVB_DataModel_v2.9.xlsm">Download_file</a>
</td>
<td><input checked="checked" id="file_names_" name="file_names[]" type="checkbox" value="11" />
</td>
</tr>
<tr>
<td>RDK_v12</td>
<td><a href="/uploads/data_model_file/attachment/12/RDKVB_DataModel_v2.9.xlsm">Download_f ile</a>
</td>
<td><input checked="checked" id="file_names_" name="file_names[]" type="checkbox" value="12" /> </td>
</tr>
<tr>
<td>RDK_v13</td>
<td><a href="/uploads/data_model_file/attachment/13/RDKVB_DataModel_v2.9.xlsm">Download_file</a>
</td>
<td><input checked="checked" id="file_names_" name="file_names[]" type="checkbox" value="13" />
</td>
</tr>
</tbody>
</table>
<br>
</div>
<div class="col-md-4 col-md-offset-4" id="diff_report_div">
<button class="btn btn-primary" id="clear_selection">Clear Selection</button>
<br>
<br>
<input class="btn btn-primary" name="commit" type="submit" value="Create a Diff report" />
</div>
</form>
所以我只需要为什么当我检查多个复选框时,我无法在数组中传递多个值。
答案 0 :(得分:0)
你的问题是“html不太合适”。
这一行ruby代码在这里:
<%= check_box_tag "file_ids[]", file.id %>
构建所有复选框吗? 您是否在html中注意到它构建的内容如下所示:
<input checked="checked" id="file_names_"
<input checked="checked" id="file_names_"
<input checked="checked" id="file_names_"
即每个复选框都有相同的ID:file_names_
浏览器如何判断已选中哪些复选框?
HTML要求每个字段都有唯一 ID。例如,html应该有类似的东西:
<input checked="checked" id="file_names_1"
<input checked="checked" id="file_names_2"
<input checked="checked" id="file_names_2"
然后,浏览器可以告诉您file_names_1
和file_names_3
已经过检查,但不是file_names_2
要在rails中执行此操作,您需要将唯一的东西(在本例中为file.id)传递到复选框的名称(而不仅仅是值),例如:
<%= check_box_tag "file_ids[#{file.id}]", file.id %>
给出一个镜头,看看它是否有帮助。
注意:是的我知道很多旧的rails示例都不需要......事情已经发生了一些变化。