簡體   English   中英

Rails視圖中的AJAX 404錯誤

[英]AJAX 404 Error in Rails View

我正在嘗試使用AJAX在index頁面上為每篇文章加載評論。

我在這里想念什么?

指數:

 #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

我如何解決它?

控制台日志:

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函數更改為此,它將正常工作-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM