渲染JS.ERB会产生原始代码

执行AJAX请求时, show.js.erb呈现部分show.js.erb

我希望能在show.js.erb做的是写:

  

由于它有一个.js扩展名,我需要在JavaScript中包装它(上面的例子不会渲染部分),所以:

 '' OR (''); 

这将呈现部分但具有原始代码 – 包括HTML和JS转义。

 ('things will go back to \"normal.\"\n\n'); 

这样做的正确方法是什么?

welcome#index

 .ajax_load.article-content{ data: { 'remote-url' => article_path(@article) } } 

articles.js

  $(document).ready(function() { $('.ajax_load').each(function(index, element) { var url = $(element).data('remote-url') if (url) { $.get(url, function(responseText) { $(element).html(responseText); }) } else { console.log("missing url for ajax!") } }) }) 

这个答案属于@MrYoshiji。

阿贾克斯:

  $(document).ready(function() { $('.ajax_load').each(function(index, element) { var url = $(element).data('remote-url') if (url) { $.get(url, function(responseText) { $(element).html(responseText); }, 'html' ) } else { console.log("missing url for ajax!") } }) }) 

articles_conroller直接渲染_article partial:

 def show #respond to JS respond_to do |format| format.js { render :partial => "article" } end end 

welcome#index

  .ajax_load.article-content{ data: { 'remote-url' => article_path(@article) } } 

您必须决定部分渲染的位置。 在大多数情况下,你使用jQuery抓取页面上的容器并在其中插入部分:

 $('#article-container').html('<%= j render 'article' %>');