目標是使用AJAX爲我的index
頁面上的每篇文章加載評論。AJAX渲染400錯誤
我得到一個錯誤bad request
400:
ERROR bad URI `/comments/%3C%=%20comment.id%20%%3E?_=1457892605480'.
指數:
#welcome/index.haml
- @articles.each do |article|
= article.title
- article.comments.each do |comment|
%comment-content{ :id => "comment-<%= comment.id %>", :class => "comment-content", "data-comment-id" => "<%= comment.id %>"}
JS:
#comments.js
var loadComment = function() {
return $('.comment-content').each(function() {
var comment_id = $(this).data('comment-id');
return $.ajax({
url: /comments/+comment_id,
type: 'GET',
dataType: 'script',
error: function(jqXHR, textStatus, errorThrown) {
return console.log("AJAX Error: " + textStatus);
},
success: function(data, textStatus, jqXHR) {
return console.log("Worked OK!");
}
});
});
};
$(document).ready(loadComment);
$(document).on('page:change', loadComment);
顯示:
#comments/show.js.erb
$('#comment-<%= @comment.id %>').append('j render(@comment.content)');
路線:
resources :articles do
resources :comments do
end
end
您的網址必須是一個字符串:URL:「/評論/」 + COMMENT_ID – toddmetheny