Rails视图中的AJAX 404错误 [英] AJAX 404 Error in Rails View
本文介绍了Rails视图中的AJAX 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用AJAX在 index
页面上为每篇文章加载评论.
I'm trying to load the comment for each article on my index
page using AJAX.
我在这里想念什么?
索引:
#welcome/index.haml
- @articles.each do |article|
= article.title
- article.comments.each do |comment|
%comment-content{ :id => "comment-<%= comment.id %>", :class => "comment-block", "data-comment-id" => "<%= comment.id %>"}
控制器:
#comments_controller.rb
class CommentsController < ApplicationController
def show
respond_to do |format|
format.js { }
end
end
end
JS:
#comments.js
var loadComment;
loadComment = function() {
return $('.comment-block').each(function() {
var $comment_block;
$comment_block = $(this);
return $.ajax('/comments/show', {
type: 'GET',
dataType: 'script',
data: {
comment_id: $comment_block.data('comment-id')
},
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)');
因此控制台日志显示以下链接,但我猜正确的URL为 localhost:3000/articles/1/comment/1
So the console log displays the following link but I guess the correct URL would be localhost:3000/articles/1/comment/1
我该如何解决?
控制台日志:
http://localhost:3000/show?comment_id=%3C%25%3D+comment.id+%25%3E&_=1457784667124
routes.rb
resources :articles do
resources :comments do
end
end
推荐答案
如果将loadComment函数更改为此,它将起作用-
If you change your loadComment function to this, it should work --
loadComment = function() {
return $('.comment-block').each(function() {
var $comment_block;
$comment_block = $(this);
comment_id: $comment_block.data('comment-id')
return $.ajax('/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!");
}
});
});
};
显示"操作的路线为/comments/:comment_id
这篇关于Rails视图中的AJAX 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文