Rails change boolean value with checkbox and jquery ajax

后端 未结 2 495
广开言路
广开言路 2020-12-14 11:11

I am new to rails and trying to change the value of a boolean via a checkbox and using jquery ajax:

<%- @tasks.each do |task| %>
  
2条回答
  •  佛祖请我去吃肉
    2020-12-14 11:48

    As of Rails 4, there's a way to do this without needing any additional JS or CSS:

    <%= check_box_tag 'completed', task.id, task.completed,
          data: {
            remote: true,
            url: url_for(action: :toggle, id: task.id),
            method: "POST"
          } %>
    

    It turns out that adding remote: true to an input causes jquery-ujs to make it ajax-y in all the nice ways. Thoughtbot's "A Tour of Rails jQuery UJS" briefly touches this (and many other good things available); the "Unobtrusive scripting support for jQuery" page in the jQuery UJS wiki does a thorough job on this as well.

提交回复
热议问题