当执行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!")
}
})
})发布于 2016-03-23 19:07:56
这个答案是属于吉吉先生的。
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
endwelcome#index
.ajax_load.article-content{ data: { 'remote-url' => article_path(@article) } }发布于 2016-03-23 12:42:59
你必须决定你想让部分渲染到哪里。在大多数情况下,您可以使用jQuery抓取页面上的容器,并在其中插入以下部分:
$('#article-container').html('<%= j render 'article' %>');https://stackoverflow.com/questions/36178135
复制相似问题