使用Ajax销毁注释时模板缺少错误

时间:2015-08-18 01:19:04

标签: ruby-on-rails ruby ajax

所有

删除评论时,我的评论控制器中的destroy方法找不到相应的视图。我仔细检查了我的destroy.js.erb文件,它看起来是正确的。我也仔细检查了评论控制器,我找不到任何东西。在我的_comment.html.erb文件中,我已将远程设置为true,因此它应该找到相应的应用程序视图以进行选择。任何人都可以提供任何指导吗?

错误消息

SDKs

comments_controller.rb

Missing template comments/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby]}.

destroy.js.erb

      class CommentsController < ApplicationController
       def create
       @post = Post.find(params[:post_id])
       @comment = @post.comments.new(params.require(:comment).permit(:body))
       @comment.user = current_user
       authorize @comment

         if @comment.save #save the code down in the database
        flash[:notice] = "Your comment was saved!"
        else
           flash[:error] = "Your comment failed to save."
        end

       redirect_to [@post.topic, @post]
     end

     def destroy
       @post = Post.find(params[:post_id])
       @comment = @post.comments.find(params[:id])

       authorize @comment
          if @comment.destroy
             flash[:notice] = "Comment was removed."
         else
             flash[:error] = "Comment couldn't be deleted. Try again."
         end
         #redirect_to [@post.topic, @post]

          respond_to do |format| #tell controller to responde via Ajax
          format.html #return response type in html
          format.js #return response type in js
         end
      end
   end

_comment.html.erb

   <% if @comment.destroyed? %>
   $('#comment-' +<%= @comment.id %>).hide();
   $('.js-comments-count').html("<%= pluralize(@comment.user.comments.count, 'comment') %>");
   <% else %>
   $('#comment-' +<%= @comment.id %>).prepend("<div class='alert alert-danger'><%= flash[:error] %>   </div");
   <% end %>

0 个答案:

没有答案