1

The object Task contains a boolean field complete. How to change the status of the object by pressing the checkbox?

Task model:

class Task < ActiveRecord::Base
  belongs_to :project

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

  def mark_complete!
    self.update_attribute(:done, true)
  end

end

Task Controller:

def done
    @task = Task.find(params[:id])
    @task.mark_complete!
  end

routes.rb:

get '/done/:id', to: 'tasks#done', as: 'done'

done.js.erb: $('#row_<%= task.id %>').css("background-color", "yellow");

View task#_task.html.erb:

<tr id="row_<%= task.id %>" data-item-id=<%= "#{task.id}" %> class="item">
  <td><%= task.name %></td>
  <td><%= task.done %></td>
  <td><%= link_to "Mark Complete", done_path(task), remote: true %></td>
  <td><%= link_to '<i class="glyphicon glyphicon-trash"></i>'.html_safe, task_path(task), remote: true,
                  class: 'btn btn-xs',
                  method: :delete,
                  data: {confirm: 'Are you sure?'} %></td>
</tr>
9
  • Possible duplicate of stackoverflow.com/q/15572371/5076451 Commented Mar 3, 2016 at 12:38
  • By pressing what checkbox? Can you show us the code you have for what you've tried so far? Commented Mar 3, 2016 at 12:43
  • @jeffdill2 Yes, I've updated the question Commented Mar 3, 2016 at 12:56
  • @AlexandrDmitrenko much better. :-) Commented Mar 3, 2016 at 12:57
  • @AlexandrDmitrenko I still don't see a checkbox. I assume you mean you want to change the status of the Task object by pressing the "Mark Complete" button? Commented Mar 3, 2016 at 13:00

2 Answers 2

1

You could do something like this:

routes.rb

resources :tasks do
  collection do
    put :mark_complete
  end
end

tasks_controller.rb

def mark_complete
  task = Task.find(params[:task_id])
  
  if task.mark_complete!
    render json: task.done?, status: :ok
  else
    render json: task.errors, status: :unprocessable_entity
  end
end

task.html.erb

...

<td><%= link_to "Mark Complete", "javascript:void(0)", class: "mark-complete", data_task_id: task.id %></td>

...

<script>
  $(document).ready(function() {
    $(document).on('click', '.mark-complete', function() {
      var taskId = $(this).data('taskId');

      $.ajax({
        url: "#{mark_complete_tasks_path}",
        type: 'PUT',
        data: { task_id: taskId },
        success: function(response, status, xhr) {
          // Whatever you want to do here with the item that was marked complete.
        },
        error: function(xhr, status, error) {
          console.log(xhr, status, error);
        },
      });
    });
  });
</script>
Sign up to request clarification or add additional context in comments.

9 Comments

Routing Error: No route matches [PUT] "/tasks"
What is the output of rake routes | grep task for you?
routes. And please tell me, what was specified in my routes.rb: scope :complete, -> { where(done: true) } scope :incomplete, -> { where(done: nil) } def mark_complete! self.update_attribute(:done, true) end > I remove it?
I'm not sure I understand your question.
Your routes look good. Where is the No route matches... error occuring?
|
0

routes.rb:

resources :tasks do
    member do
      post 'done'
    end
  end

Controller:

def done
    @task = Task.find(params[:id])
    @task.update_attributes(:done => params[:done])

    respond_to do |format|
      format.js {}
    end

  end

View:

....
 <td><%= task.done %></td>
 <td><%= check_box_tag 'done', task.id , task.done, :class => "task-check", remote: true %></td>
....

<script>
  $(".task-check").bind('change', function(){
    $.ajax({
      url: '/tasks/'+this.value+'/done',
      type: 'POST',
      data: {"done": this.checked},
    });
  });

</script>

tasks#done.js.erb: window.location.reload();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.