I am trying to have a div display the contents of a comment. Then when you click on the comment you can do some inline editing of the text via a form field. I am hiding the form and display the comment when the page loads. When you click on the comment I want to hide the comment and display the form.
When I click on the comment I get this error:
Error: Missing target element "inlineEdit.form"
Here is my form:
<%= form_for([@servicerequest, @comment || @servicerequest.comments.build], remote: true, class: local_assigns[:class],
data: {target: "inlineEdit.form"}) do |f| %>
<div>
<%= error_messages_for(f.object) %>
</div>
<div>
<%= f.text_area :content, class: 'form-control', autofocus: true %>
</div>
<div style="padding-top:20px;">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
This is my comment partial:
<div class="feed-element" id="<%= dom_id(comment) %>" data-controller="inlineEdit">
<div class="media-body" style="font-size:14px;">
<span class="pull-right text-navy"><%= time_ago_in_words(comment.updated_at) %> ago</span>
<strong><%= comment.user.username %></strong> updated this service request.
<br>
<small class="text-muted"><%= @servicerequest.created_at.to_time.strftime('%B %e at %l:%M %p') %></small>
<div class="well" style="font-size:14px;">
<div data-target="inlineEdit.content" data-action="click->inlineEdit#toggle"><%= comment.content.html_safe %></div>
<% render "comments/form", comment: comment, class: "d-none" %>
<% if current_user.username == comment.user.username %>
<div class="actions">
<%= link_to "Edit", edit_servicerequest_comment_path(comment.servicerequest, comment), :remote => true, class: 'btn btn-xs btn-primary' %>
<%= link_to "Delete", [@servicerequest, comment] , method: :delete, :remote => true, data: { confirm: 'Are you sure?' }, class: 'btn btn-xs btn-outline btn-inverse' %>
</div>
<% end %>
</div>
</div>
</div>
This is inlineEdit_controller.js
import { Controller } from "stimulus"
export default class extends Controller {
static targets = [ "content", "form" ]
toggle() {
this.contentTarget.classList.toggle("d-none")
this.formTarget.classList.toggle("d-none")
}
}
Any ideas why I am getting the error that the form target element is missing?