0

为什么渲染从文件:locals调用时为什么不起作用?js.erb

这是我的create.js.erb

$("#div").before("<%= escape_javascript(render("/admin/products/variant_image_preview", :locals => {:image => @variant_image})) %>");

variant_image_preview.html.erb

<div id="variant_image_<%= image.id %>">

  <%= image_tag image.image_url(:thumb), width: 114, height: 88 %>
  <% form_name = "product[variants_attributes][1][images_attributes][#{Time.now.to_f}]" %>

  <input type="hidden" value="<%= image.id %>" name="<%= form_name %>[id]">

  <a id="<%= image.id %>" class="remove_image" title="Remover imagem">
    <img src="/assets/bg_close.png" alt="Remover imagem">
  </a>
</div>

在variant_image_preview.html.erb 的第一行我得到这个错误:

NameError - undefined local variable or method `image' for #<#<Class:0x0000000f4f6e88>:0x000000101332c8>:

StackOverflow 上的一些帖子说这段代码应该可以工作。有什么帮助吗?

4

1 回答 1

1

在您使用的语法中,locals 应该作为第二个参数传递,如下所示:

render("/admin/products/variant_image_preview", :image => @variant_image)

您可以用更具声明性的调用替换此语法:

render(partial: "/admin/products/variant_image_preview", :locals => {:image => @variant_image})
于 2013-02-15T20:47:02.347 回答