当 AJAX 请求执行时,show.js.erb
呈现部分 _article.haml
。
我希望能够在 show.js.erb
中编写:
<%= j render 'article' %>
由于它有一个 .js
扩展名,我需要将其包装在 JavaScript 中(上面的示例不会渲染部分),因此:
'<%= j render 'article' %>' OR ('<%= j render 'article' %>');
这将渲染部分但带有 原始代码——包括 HTML 和 JS 转义。
('things will go back to \"normal.\"<\/p>\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。
Ajax :
$(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
部分:
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) } }
关于javascript - 渲染 JS.ERB 结果为原始代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36178135/