【问题标题】:fix the render with ajax in partial在部分中使用 ajax 修复渲染
【发布时间】:2016-11-22 19:42:18
【问题描述】:

我正在尝试渲染另一个部分,如下所示:

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

但我收到此错误:

ActionView::Template::Error(未定义的局部变量或方法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'

无法识别变量“cobran”,因为在视图中以这种方式进行渲染:

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

并且在部分中放置“cobranza”的单数,即“cobran”,如下所示:

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


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

</tr>

我该如何解决这个问题?谢谢

【问题讨论】:

    标签: javascript jquery ruby-on-rails ajax ruby-on-rails-4


    【解决方案1】:

    您不能在 javascript 中使用该 erb 变量。通过数据属性将其传递给 javascript。添加到您的元素之一:data-cobran="&lt;%= @detalleco.cobran %&gt;" 在元素上放置一个 id,然后通过 id var cobran = $("#nameOfId").data('cobran') 访问它

    之后就可以使用了。

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      相关资源
      最近更新 更多