如何在一个多记录Rails的形式设置复选框一个唯一的ID? [英] How do I set a unique ID for checkboxes in a multi-record Rails form?
问题描述
我已经成立了一个Rails的形式大致按照这个 Railscast。
这里的code的形式:
<%的form_tag complete_todos_path,:方法=> :把做%>
< UL>
< DIV ID =incomplete_todos>
<%@ incomplete_todos.each办|待办事项| %>
<%=渲染:部分=>待办事项%>
<%结束%GT;
< / DIV>
< / UL>
<%= submit_tag为完成标志%>
<%结束%GT;
和这里的code的TODO部分:
< DIV CLASS =待办事项>
<李>
<%= check_box_tagtodo_ids [],todo.id%>
&其中;%= H todo.name%GT;
<%=的link_to'编辑',edit_todo_path(待办事项)%>
<%=的link_to删除,待办事项,:确认=> 你确定吗?,:方法=> :删除%>
< /李>
< / DIV>
它的工作很大,但我希望开始实现Ajax,我需要每个复选框有一个唯一的ID。眼下,输入标签产生看起来是这样的:
<输入ID =todo_ids_NAME =todo_ids []类型=复选框值=7/>
每复选框具有相同的ID(todo_ids_),这是一个问题。我怀疑的解决方案是令人尴尬的简单,但我没有看到它。任何提示?
解决方案我结束了使用类似瑞安的一个解决方案,但正如我在评论中写道,我不得不做出进一步的改变
<%= check_box_tagtodo_ids [#{todo.id}],todo.id%>
在调用的形式操作:
Todo.update_all([,Time.nowcompleted_at =?]:n => PARAMS [:todo_ids]。键)
请注意在PARAMS [:todo_ids]。键结尾,这是一个解决办法来处理与奇方式的参数进行了格式化的:
todo_ids=> {5=>中5}
I've set up a Rails form roughly following the instructions in this Railscast.
Here's the code for the form:
<% form_tag complete_todos_path, :method => :put do %>
<ul>
<div id="incomplete_todos">
<% @incomplete_todos.each do |todo| %>
<%= render :partial => todo %>
<% end %>
</div>
</ul>
<%= submit_tag "Mark as completed" %>
<% end %>
And here's the code for the todo partial:
<div class="todo">
<li>
<%= check_box_tag "todo_ids[]", todo.id %>
<%=h todo.name %>
<%= link_to 'edit', edit_todo_path(todo) %>
<%= link_to 'delete', todo, :confirm => 'Are you sure?', :method => :delete %>
</li>
</div>
It's working great, but I'm looking to start implementing AJAX and I need each checkbox to have a unique id. Right now, the input tags generated look something like this:
<input id="todo_ids_" name="todo_ids[]" type="checkbox" value="7" />
Every check box has the same id ("todo_ids_"), which is a problem. I suspect the solution is embarrassingly simple, but I'm not seeing it. Any tips?
I ended up using a solution similar to Ryan's, but as I wrote in the comment I had to make a further change. In the form:
<%= check_box_tag "todo_ids[#{todo.id}]", todo.id %>
In the action called by the form:
Todo.update_all(["completed_at = ?", Time.now], :id => params[:todo_ids].keys)
Note the "params[:todo_ids].keys" at the end, which was a workaround to deal with the odd way the parameters were formatted:
"todo_ids" => {"5"=>"5"}
这篇关于如何在一个多记录Rails的形式设置复选框一个唯一的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!