简体   繁体   English

通过JavaScript从元素列表调用Rails操作

[英]Call rails action from list of elements through javascript

My end goal is to have a user drag items from a list and place it in one of 5 "buckets". 我的最终目标是让用户从列表中拖动项目并将其放在5个“存储桶”之一中。 Imagine a list of animals, and you drag dogs and cats into a mammals list, and snakes and iguana to a reptile list, etc. 想象一下动物列表,然后将狗和猫拖到哺乳动物列表中,然后将蛇和鬣蜥拖到爬行动物列表中,等等。

Using HTML5 Sortable, I am able to have someone drag the item to each list, but it doesn't save the change. 使用HTML5 Sortable,我可以让某人将该项目拖到每个列表中,但是不会保存更改。 How do I have javascript call the action or route to save the item once it is moved? 移动项目后,我如何用javascript调用操作或路线来保存项目?

This is my main code 这是我的主要代码

        <% @cabins.each do |cabin| %>
          <li class="highlight" draggable="true" style="display: list-item;"><%= cabin.name %>-<%= Room.where(name: cabin.name).sum(:clean_time).to_i %></li>
        <% end %>

    </ul>
    Bucket 1
    <ul id="sortable5" class="connected sortable list">
        <li class="highlight" draggable="true">Item 1
        </li><li draggable="true" class="" style="display: list-item;">Item 2
        </li><li draggable="true" class="" style="display: list-item;">Item 3
        </li><li draggable="true" class="" style="display: list-item;">Item 4
        </li><li class="highlight" draggable="true">Item 5
    </li></ul>
    Bucket 3
    <ul id="sortable5" class="connected sortable list">
        <li class="highlight" draggable="true">Item 1
        </li><li draggable="true" class="" style="display: list-item;">Item 2
        </li><li draggable="true" class="" style="display: list-item;">Item 3
        </li><li draggable="true" class="" style="display: list-item;">Item 4
        </li><li class="highlight" draggable="true">Item 5
    </li></ul>
</section>

This is the javascript magic $(function() { 这就是javascript的$(function(){

        $('#sortable4, #sortable5').sortable({
            connectWith: '.connected'

        });
    });
</script>

I tried using the code below to call Ajax, but how do I pass a rails variable to it? 我尝试使用下面的代码调用Ajax,但是如何向其传递rails变量? And what would the route look like? 路线是什么样的? $.ajax({ type: "PUT", url: "/cabins/<%= cabin.id %>" }); $ .ajax({type:“ PUT”,url:“ / cabins / <%= cab.id%>”});;

There are couple of jQuery events in sortable that can be used here. 有几个sortable的jQuery事件可在此处使用。

  1. change 更改

     $( ".selector" ).on( "sortchange", function( event, ui ) {} ); 
  2. sort 分类

     $( ".selector" ).on( "sort", function( event, ui ) {} ); 

Please take a look at this documentation for further help 请查看此文档以获取更多帮助

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

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