我在js.erb文件中有以下内容:
//add an image from the gallery to the mail later
$('.uploaded_image').click(function(){
var src=$(this).attr('src').replace("thumb", "medium");
var location = $('#user_image_location').attr('value');
$('#mailing_body').contents().find("[data-edit-img="+''+location+''+"]").attr('src', src);
$('#image_form').html('<%=render "user_images/form"%>');
$('[data-dismiss]="cancel"').click();
})
我以为我可以在js文件中使用erb来替换页面上的内容。 我收到以下错误:
undefined method `render' for #<#<Class:0x00000103dea400>:0x000001059f2198>
如果我删除以下行:
$('#image_form').html('<%=render "user_images/form"%>');
一切正常。
我以为我可以在带有Rails 3.2的js文件中使用erb
为什么这不起作用?
提前致谢。
答案 0 :(得分:2)
你肯定可以在你的.js.erb文件中使用渲染(包括任何&所有助手,资源等)(我只是验证了确定)。
要.js.erb
个文件的处理方式与.html.erb
个文件相同,必须将它们放在views
文件夹中(例如app/views/<controller>
或app/views/shared
),它们不能放在你的app/assets/javascripts
文件夹中。