У меня есть частичная форма с формой и кнопкой. Я делаю это частично с помощью файла activate_item.js.erb и хотел бы иметь доступ к переменной экземпляра в представлении индекса.переменная обновления для файла js.erb в цикле
activate_item.js.erb:
<%= form_for :c, url: activate_item_item_url(c.id), :html => {:id => "cc_form_#{c.id}", :class => "cc_form_class"}, :method => "put", :remote => true do |k| %>
<%= button_to "#{c.recyclable}", {:url => "activate_item_item_url", :controller => :items, :action => :activate_item, :id => "item_recyclable_#{c.id}"}, :class => 'btn btn-success btn-sm' %>
<% end %>
index.html.erb:
<% @items.each do |c| %>
<tr>
<td><%= c.description %></td>
<td><%= c.color %></td>
<% if c.active_recyclable_second %>
<td><%= button_to "ON", {:controller => :items, :action => :activate_recyclable_second, :id => c.id}, :method => :put, :class => 'btn btn-danger btn-sm' %></td>
<% else %>
<td><%= button_to "OFF", {:controller => :items, :action => :activate_recyclable_second, :id => c.id}, :method => :put, :class => 'btn btn-danger btn-sm' %></td>
<% end %>
<div id="cc_form_ext_#{c.id}">
<td><%= render "items/recyclable_update", c: c %></td>
</div>
</tr>
<% end %>
activate_item.js.erb:
$('#cc_form_ext_<%= @item.id %>').append('<%= escape_javascript(render 'recyclable_update') %>');
Я читал, что мне нужно для обновления переменной c новым вызовом ajax. Rails 3: Instance variable inaccessible in .js.erb file, но я не уверен, как это сделать. Мы ценим любые предложения.
ActionView :: Template :: Error (неопределенная локальная переменная или метод 'c 'для # <# <Класс: 0 ...............>): <% = form_for: c, url: activate_item_item_url (c.id),: html => {: id => "cc_form _ # {c.id}",: class%> и т. д. – dennis
Я нашел его. Я синтаксически пропустил в js.erb. $ ('# cc_form_ext _ <% = @ item.id%>'). Html ('<% = escape_javascript (render' recyclable_update ', c: @item)%>'); – dennis