I’m new to the world of Stimulus, I’ve been working on adding a few actions to my Rails application. I have a comments form, when submitted I’d like the comment body from the form to be appended to the rest of the comments. Everything seems to be working because this happens, but then the controller action runs again giving the appearance that the comment was added two times.
This is my controller:
import { Controller } from "stimulus"
export default class extends Controller {
static targets = ["body", "add"]
addBody() {
let content = this.bodyTarget.value;
this.addTarget.insertAdjacentHTML('beforebegin', `<div>${content}</div>`);
}
}
The form:
<div data-controller="posts">
<div data-target="posts.add">
</div>
<!-- this is from a rails partial but it's all inside the data-controller -->
<%= form_with scope: :post, url: posts_path, method: 'post', data: { action: "post#addBody" } do |form| %>
<%= form.text_field :content, class: "form-control", data: { target: "posts.body"} %>
<%= form.submit class: "btn btn-primary" %>
</div>
For general reference my Rails controller:
def create
@post = current_user.post.build(post_params)
respond_to do |format|
if @post.save
format.json {head :ok}
else
raise ActiveRecord::Rollback
end
end
end
I’ve tried several things, explicitly setting remote: true
on the form in the view, also trying data: { action: "ajax:success->post#addBody" }
these things don’t seem to make an impact. Adding a debugger statement to the controller action I can clearly see that the action is being called two times.