Rails - How to access index value in partial

926 Views Asked by At

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?

2

There are 2 best solutions below

0
yeuem1vannam On BEST ANSWER

In your create.js.erb, there is an undefined variable index at $('#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:

<!-- app/views/clients/index.html.erb -->
<% @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}" %>
    <!-- Letting server know the index of current form by adding a param -->
    <%= hidden_field_tag :client_index, index %>
    <%= f.submit("New Communication") %>
  <% end %>
<% end %>

Then at your js.erb file, use params[:client_index] instead of index

# app/views/communications/create.js.erb
$('#communication_pane<%= params[:client_index] %>')
  .prepend('<%= escape_javascript(render @communications) %>');

Hope this help.

0
H Dox On

I think you will have to pass index variable to your controller action as hidden_field and then display it on create.js.erb

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.hidden_field :index, value: index %>
  <%= f.text_area :content, class: "form-control", id: "communication_content#{index}" %>
  <%= f.submit("New Communication") %>
<% end %>

communications_controller

def create
  @index = params[:communication][:index]
  ...
end

Communications/create.js.erb

$('#communication_pane<%= @index %>').prepend('<%= escape_javascript(render @communications) %>');