Reputation:
When an AJAX request executes, show.js.erb
renders the partial _article.haml
.
What I want to be able to do in show.js.erb
is to write:
<%= j render 'article' %>
Since it has a .js
extension I am required to wrap this in JavaScript (the example above does not render the partial), so:
'<%= j render 'article' %>' OR ('<%= j render 'article' %>');
This would render the partial but with raw code--including HTML and JS escaping.
('things will go back to \"normal.\"<\/p>\n\n');
What's the right way to do this?
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!")
}
})
})
Upvotes: 1
Views: 1022
Reputation:
This answer belongs to @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
renders _article
partial directly:
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) } }
Upvotes: 1
Reputation: 4475
You have to decide where you want the partial to render. In most cases, you'd grab the container on the page with jQuery and insert the partial there like:
$('#article-container').html('<%= j render 'article' %>');
Upvotes: 0