form_for called in a loop overloads IDs and associates fields and labels incorrectly
Posted
by
Katy Levinson
on Stack Overflow
See other posts from Stack Overflow
or by Katy Levinson
Published on 2011-02-18T06:59:02Z
Indexed on
2011/02/18
7:25 UTC
Read the original article
Hit count: 160
Rails likes giving all of my fields the same IDs when they are generated in a loop, and this causes trouble.
<% current_user.subscriptions.each do |s| %>
<div class="subscription_listing">
<%= link_to_function s.product.name, "toggle_delay(this)"%>
in <%= s.calc_time_to_next_arrival %> days.
<div class="modify_subscription">
<%= form_for s, :url => change_subscription_path(s) do |f| %>
<%= label_tag(:q, "Days to delay:") %>
<%= text_field_tag(:query) %>
<%= check_box_tag(:always) %>
<%= label_tag(:always, "Apply delay to all future orders") %>
<%= submit_tag("Change") %>
<% end %>
<%= link_to 'Destroy', s, :confirm => 'Are you sure?', :method => :delete %>
</div>
</div>
<% end %>
Produces
<div class="subscription_listing">
<a href="#" onclick="toggle_delay(this); return false;">Pasta</a>
in 57 days.
<div class="modify_subscription">
<form accept-charset="UTF-8" action="/subscriptions/7/change" class="edit_subscription" id="edit_subscription_7" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="s5LJffuzmbEMkSrez8b3KLVmDWN/PGmDryXhp25+qc4=" /></div>
<label for="q">Days to delay:</label>
<input id="query" name="query" type="text" />
<input id="always" name="always" type="checkbox" value="1" />
<label for="always">Apply delay to all future orders</label>
<input name="commit" type="submit" value="Change" />
</form> <a href="/subscriptions/7" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
</div>
</div>
<div class="subscription_listing">
<a href="#" onclick="toggle_delay(this); return false;">Gummy Bears</a>
in 57 days.
<div class="modify_subscription">
<form accept-charset="UTF-8" action="/subscriptions/8/change" class="edit_subscription" id="edit_subscription_8" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="s5LJffuzmbEMkSrez8b3KLVmDWN/PGmDryXhp25+qc4=" /></div>
<label for="q">Days to delay:</label>
<input id="query" name="query" type="text" />
<input id="always" name="always" type="checkbox" value="1" />
<label for="always">Apply delay to all future orders</label>
<input name="commit" type="submit" value="Change" />
</form> <a href="/subscriptions/8" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
</div>
</div>
And that's a problem because now no matter which "Apply delay to all future orders" I select it always very helpfully checks the first box for me. How can I override the ID without doing something ugly and un-rails-like?
© Stack Overflow or respective owner