Rails check_box_tag how to pass value when checked ajaxily

后端 未结 4 1829
死守一世寂寞
死守一世寂寞 2021-01-01 03:58

On my index page for my Task model, I want to show a checkbox for every row that corresponds to the boolean field \"complete\" in my Task database table.

Currently m

4条回答
  •  一个人的身影
    2021-01-01 04:38

    check_box_tag 'complete', task.complete ? 'false' : 'true', task.complete, ...
    :url => url_for( :action => 'complete', :id => task.id )
    

    This way in your controller you can get params[:complete]. And you should implement complete.js.erb to rerender checkbox, so next click will send inverse value

    Or you can implement js on click event

    $('.input-large').on('click', function() {
      $.ajax({
        type: "PUT",
        url: "/tasks/complete/" + $(this).data('post-id')
        data: { complete: $(this).is(':checked') }
      });
    });
    

    and don't forget to place data-post-id param to your checkbox

提交回复
热议问题