简体   繁体   中英

How to post a toggle to rails via jquery/javascript

I currently have a model called project_todo and it has a column called done that is a boolean. I have it setup so I can currently drag this from one side of the app to the other, dragging it from todo to done. However, it doesn't save obviously.

What I need to do is on drag toggle the boolean and I believe what I need to do is post via JS/jquery to the update path to do it. However, I'm not exactly sure what this should look like.

Can anyone give me some example code and/or point me in the right direction.

So I could do something like this:

$.ajax({
  type: 'POST',
  url: '/project_todo/<%project_todo.id%>/edit',
});

let's say we have a link that clicking on it should update the column:

<%= link_to 'update project', edit_project_todo_path(project_todo), :class => 'updateProject' %>

then js may look like:

$('.updateProject').click(function(event) {
  event.preventDefault();

  $.ajax({
    type: 'POST',
    url: $(event.target).attr('href')
  });
});

You can do this with any event you want, if it's a form you can use action attribute of the form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM