I need to update value isCompleted in my model. But my code works only for todo item with id=1. If i click to another checkbox of todo item id remains the same (id=1). How to connect my checkbox for another items.
class TodosController < ApplicationController
def create
@todo = Todo.create(todo_params)
if @todo.save
redirect_to root_path
else
end
end
def update
@todos = Todo.find_by(params.require(:todos).permit(:id))
@todoo = @todos.update(bool)
redirect_to root_path
end
private
def todo_params
params.require(:todo).permit(:text, :project_id) # add any other attributes you want
end
def bool
params.require(:todos).permit(:isCompleted)
end
end
index.html.erb
<% @projects.each do |project| %>
<div class="col-md-6 col-lg-4">
<div class="todo">
<table>
<tr class="border_bottom">
<td>
<h2><%= project.title %></h2>
</td>
</tr>
<tr>
<td>
<ul>
<% project.todos.all.each do |todo| %>
<li>
<%= form_for :todos, :url => todos_update_path, method: :patch, html: {id: "update" } do |f| %>
<p>
<%=f.check_box(:isCompleted,{class: 'icheckbox_square-blue', checked: todo.isCompleted},todo.id) %>
<%= todo.text %>
<%= f.hidden_field :id, :value => todo.id %>
</p>
<% end %>
</li>
<% end %>
</ul>
</td>
</tr>
</table>
</div>
</div>
<% end %>
application.js
$(".icheckbox_square-blue").change( function(){
$('#update').submit();
Aucun commentaire:
Enregistrer un commentaire