为什么在从: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上的一些帖子说这段代码应该可行。有什么帮助吗?
答案 0 :(得分:1)
在您使用的语法中,应将locals作为第二个参数传递,如下所示:
render("/admin/products/variant_image_preview", :image => @variant_image)
您可以使用更多声明性调用替换此语法:
render(partial: "/admin/products/variant_image_preview", :locals => {:image => @variant_image})