samedi 1 août 2015

How to move an object to another scope through checkbox using Ajax?

I've lost in thought. I have a checkbox that I want to use to transfer a corresponding object to another scope on my page. Moreover, I want only to transfer one field of this checked object. I had used earlier Ajax by creating a .json file and then I responded to it in my controller. In my case input checkbox doesn't seem to have a remote: true option.

views/tasks/_index.html

<h3>Tasks database</h3>

<table>

  <% @tasks.each do |task| %>
    <tr class='tasks' id="task_<%= task.id %>">

    #Some stuff

      <td>
        <input type="checkbox" class='check' data-id="<%= task.id %>" >
      </td>

    </tr>

  <% end %>

</table>

<h3>Completed</h3>

<div class="complete-tasks">
</div>

Hence, I'm trying to accomplish that using an event via javascript.

So far I've managed to write some javascript code that moving my entire object.

application.js

  $(document).on('ready page:load', function () {
        $('.check').on('change', function(){
        if ($(this).is(':checked')) {
        id = $(this).attr('data-id');
        task = $(this).closest("tr");
        $('.complete-tasks').last().after(task);
        }
      });
    })

enter image description here

But I want to relocate only one 'title' field using Ajax. Can someone please explain me how to accomplish that? I suspect I need to pass in some id's in checkbox and to define $.ajax.

models/task.rb

class Task < ActiveRecord::Base

  scope :complete, -> { where(complete: true) }
  scope :incomplete, -> { where(complete: nil) }

  belongs_to :user
end




Aucun commentaire:

Enregistrer un commentaire