So I am trying to create a list of tasks within a goal and in order to complete them, I am creating a form_for with a hidden field that will update the value for the each task record when clicking submit. I'm trying to add ajax to the form so that when a user hits submit, it will update the record for the task and it will render a partial replacing this form with a div saying "complete!".
goals_controller.rb
def show
@goal = Goal.where(user_id: current_user, id: params[:id]).first
@goal_tasks = @goal.tasks
# This is where the AJAX request will happen
respond_to do |format|
format.html {}
format.js {}
end
end
show.html.erb for Goals
<% @goal_tasks.each do |task| %>
<div class="complete-button">
<% if task.completed == false %>
<%= form_for([@goal,task], remote: true) do |f| %>
<%= f.hidden_field :completed , :value => true %>
<%= f.submit "Complete" , :class => "submit-complete" %>
<% end %>
<% else %>
<%= render partial: "goals/taskcompleted" %>
<% end %>
<div>
<% end %>
show.js.erb
$(".complete-button").html("<%= j(render partial: 'goals/taskcompleted') %>")
The behavior that is happening is that if there are multiple tasks, there are multiple forms with unique id="edit_task_unique_id" in the html but since I'm grabbing the .complete-button class in the show.js.erb. It is replace all of the forms with the complete div. I've tried to somehow grab the unique id's the form creates with each task but no luck. Any help or feedback is appreciated!