我已經大致按照this Railscast的說明設置了一個Rails窗體。如何在多記錄Rails表單中設置複選框的唯一ID?
下面是窗體的代碼:
<% 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 %>
而這裏的待辦事項部分代碼:
<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>
它的工作很好,但我期待開始實施AJAX,我需要每個複選框都有一個唯一的ID。現在,生成的輸入標籤看起來像這樣:
<input id="todo_ids_" name="todo_ids[]" type="checkbox" value="7" />
每個複選框都有相同的id(「todo_ids_」),這是一個問題。我懷疑這個解決方案很簡單,但我沒有看到它。有小費嗎?
生成以下內容: <輸入的ID = 「todo_ids_7」 名稱= 「todo_ids [7]」 類型= 「複選框」 值= 「7」/> 的ID都是唯一的,這是很大的,但它發送在不可思議的方式的參數: todo_ids 「=> {」 5 「=>」 5 「} 我最終使用: Todo.update_all([」 completed_at =「,Time.now?],: id => params [:todo_ids] .keys)作爲一種解決方法,它工作得很好(雖然它看起來像一種凌亂的解決方案)。謝謝! – 2009-06-14 05:31:25
你應該使用params [:todo_ids] .keys.collect(&:to_i) 。 真高興你做到了。 – 2009-06-14 06:12:35