On May 1, 10:24 pm, Tom DeHart <li...@ruby-forum.com> wrote:
> 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())
> });
Have you checked the browser's JavaScript console for any errors? That
last line looks suspicious to me. I would have thought $
(value.form).submit() would suffice.
Also I think this general approach might be built on slightly shaky
approach - Since the whole form is being submitted each time, the last
request (for example) is probably going to be sending all the
information from the previous iterations (and of course there's no
guarantee they'll get processed in exactly the same order they were
sent in - a true vote on the 5th item could get overwritten when the
form is submitted for the 4th time). Personally I'd build up a single
hash of all the data I wanted to submit and use $.post to send it.
Fred
>
> });
>
> $("#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?
>
> --
> Posted viahttp://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment