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>
Taskobject by pressing the "Mark Complete" button?