fix the render with ajax in partial

2019-09-10 22:06发布

问题:

I am trying to render in another partial, like this:

<% cobran = @detalleco.cobran %>
$("#cobran_<%= @detalleco.IdCobranza %>").fadeOut(500, function(){
  $(this).remove();
  $(".child").remove();
  $("#container_cobranza").html("<%=escape_javascript(render(:partial => 'cobranza/cobran', cobranza: cobran))%>");
});

but I get this error:

ActionView::Template::Error (undefined local variable or method cobran' for #<#<Class:0xb47692ec>:0x83cb1500> Did you mean? cobran_url): 1: <tr id="cobran_<%= cobran.id %>"> 2: <td><%=cobran.id%> 3: 4: app/views/cobranza/_cobran.html.erb:1:in_app_views_cobranza__cobran_html_erb__36360536__1042659538' app/views/detallecob/create.js.erb:12:in `_app_views_detallecob_create_js_erb__76211164__1041949938'

don't recognize the variable "cobran" because in the view is doing the render this way:

<tbody id="container_cobranza">
      <%= render @cobranza %>
</tbody>

And in the partial puts the singular of "cobranza" that is "cobran" like this:

<tr id="cobran_<%= cobran.id %>">
  <td><%=cobran.id%>


  <td><%=cobran.FechaReg%></td>
  <td><%=cobran.FechaVence%></td>
  <td><%=cobran.TipoDoc%></td>

</tr>

How can I solve this issue? thanks

回答1:

You can't use that erb variable in javascript. Pass it to the javascript via a data attribute. Add to one of your elements: data-cobran="<%= @detalleco.cobran %>" Put an id on the element and then access it by the id var cobran = $("#nameOfId").data('cobran')

After that you can use it.