简体   繁体   English

如何通过jquery / javascript将切换发布到rails

[英]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. 我目前有一个名为project_todo的模型,它有一个名为done的列,它是一个布尔值。 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. 我需要做的是拖动布尔值,我相信我需要做的是通过JS / jquery将消息发布到更新路径上。 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: 那么js可能看起来像:

$('.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 您可以对任何事件进行此操作,如果是表单,则可以使用表单的action属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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