First, a picture of what I'm doing: http://i.imgur.com/MO2o8.png
A user should be able to select as many of these checkboxes and click on
the "Vote Yes" or "Vote No" links (styled as buttons) to change their
vote to yes or no (vote.approval = true or vote.approval = false).
I got it working so that I can select one box at a time and it will
submit correctly and update the vote's approval field. However, when I
select more than one, it fails to work.
Here's the code:
The View (https://gist.github.com/950886):
<% form_for(vote, :remote => true) do |f| %>
<%= check_box :selection, vote.id, {}, vote.id %>
<%= hidden_field_tag("approval_"+vote.id.to_s, "temp") %>
...
<%= link_to 'Vote Yes', ballots_path, :id => "vote_yes_top" %>
<%= link_to 'Vote No', ballots_path, :id => "vote_no_top" %>
The jQuery code (https://gist.github.com/950887):
$("#vote_yes_top, #vote_yes_bottom").click(function() {
$.each($('input[type=checkbox]:checked'), function(key, value) {
var approval_string = "approval_"+value.value
$(value.form.children(approval_string)).val(true)
$('#'+$(value.form).submit())
});
});
$("#vote_no_top, #vote_no_bottom").click(function() {
$.each($('input[type=checkbox]:checked'), function(key, value) {
var approval_string = "approval_"+value.value
$(value.form.children(approval_string)).val(false)
$('#'+$(value.form).submit())
});
});
The controller (https://gist.github.com/950889):
def update
@vote = Vote.find(params[:id])
@vote.approval = params["approval_#{@vote.id}".to_sym]
...
(standard update code)
So, like I said, I get into the update method quite nicely when I just
select one box at a time but when I select more than one it just updates
the first box's object then drops out of the .each() loop.
Anyone know why this would happen?