I am trying to use the built-in rails ajax to append an item to a list after a user fills out a form - However, I cannot target the div tag to append the new entry to: ActionView::Template::Error (undefined local variable or method 'index'
Clients/Index:
<% @clients.each_with_index do |client, index| %>
<div id="communication_pane<%= index %>">
<%= render client.communications.order(created_at: :desc) %>
</div>
<%= form_for([@clientlist, client, client.communications.build], remote: true) do |f| %>
<%= f.text_area :content, class: "form-control", id: "communication_content#{index}" %>
<%= f.submit("New Communication") %>
<% end %>
Communications/create.js.erb:
$('#communication_pane<%= index %>').prepend('<%= escape_javascript(render @communications) %>');
How can you access the index value from Clients/Index?
In your
create.js.erb, there is an undefined variableindexat$('#communication_pane<%= index %>')Because there is no way the server knows about where you clicked on the client so that you have to explicitly tell the server about it. Here is an idea:
Then at your
js.erbfile, useparams[:client_index]instead ofindexHope this help.