简体   繁体   English

无法在js.erb文件中进行用户渲染

[英]can't user render in js.erb file

I have the following in a js.erb file: 我在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();

  })

I thought I could use erb in the js file to replace content on a page. 我以为我可以在js文件中使用erb来替换页面上的内容。 I get the following error: 我收到以下错误:

undefined method `render' for #<#<Class:0x00000103dea400>:0x000001059f2198>

If I remove the following line: 如果我删除以下行:

  $('#image_form').html('<%=render "user_images/form"%>');

it all works. 一切正常。

I thought I could use erb in js files with Rails 3.2 我以为我可以在使用Rails 3.2的js文件中使用erb

Why does this not work? 为什么这不起作用?

Thanks in advance. 提前致谢。

You most certainly can use render (including any & all helpers, resources etc.) inside your .js.erb file (I just verified to be certain). 你当然可以在你的.js.erb文件中使用render(包括任何&所有助手,资源等)(我只是确认是确定的)。

For .js.erb files to be processed the same way as your .html.erb files they must be placed inside your views folder (such as app/views/<controller> or app/views/shared ), they cannot be placed in your app/assets/javascripts folder. 对于.js.erb文件的处理方式与.html.erb文件相同,它们必须放在您的views文件夹中(例如app/views/<controller>app/views/shared ),它们不能放入你的app/assets/javascripts文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM