2012-04-24 103 views
0

我有一個js.erb文件中的以下內容:不能用戶呈現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(); 

    }) 

我以爲我可以使用ERB在js文件在頁面上更換內容。 我得到以下錯誤:

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

如果我刪除以下行:

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

它所有的作品。

我認爲我可以在使用Rails js文件使用ERB 3.2

爲什麼這個不行?

在此先感謝。

回答

2

你當然可以在你的.js.erb文件中使用渲染(包括任何&所有幫助程序,資源等)(我剛剛證實是確定的)。

對於.js.erb要處理的文件的方式爲您.html.erb文件必須放在你的views文件夾內相同(如app/views/<controller>app/views/shared),它們不能被放置在你的app/assets/javascripts文件夾中。

+0

謝謝馬克,清除了 – chell 2012-04-25 05:17:53