I have tried to use ajax and jquery to render partial on-click. Please see Is it possible to render partial onclick without link_to? for details
It works, the partial got rendered but it's not working correctly.
I've placed the link_to inside a loop but when the "New Comment" is clicked, it appear on the first post, when I clicked the "New Comment" on the second/third/fourth posts, all of the partials got render on the first post. What is going on?
Please see below my code.
Post/index.html.erb
<% @posts.each do |post| %>
<%= post.title %>
<%= post.content %>
<%= link_to 'New Comment', new_comment_path, id: 'new_comment', remote: true %>
<% end %>
Comment/new.js.erb
$('#new_comment').hide().after('<%= j render("form") %>')
_form.erb
<%= form_for(@comment) do |f| %>
<div>
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>