Rails - 处理Ajax破坏多态关联的调用

时间:2012-09-06 01:44:23

标签: javascript ruby-on-rails ajax ruby-on-rails-3 polymorphic-associations

我试图通过Ajax销毁评论。在我的应用程序中,我的评论模型使用多态关联。注释使用以下代码成功删除(在数据库中)。但是,当我调用destroy.js.erb时,它没有做任何事情,我认为问题是dom_id与HTML id不匹配,所以它没有更新。我想我正在经历与answer to this question中明确指出的@mu_is_too_short相同的事情。我需要帮助解决这个问题。我不知道解决方案是否涉及a)以某种方式将局部变量注释传递给destory.js.erb或b)另一种解决方案。

的routes.rb

resources :feeds do
  resources :comments
end

destroy.js.erb

 $('#<%= dom_id(@comment) %>')
   .fadeOut ->
   $(this).remove()

_comment.html.erb

 <div id=<%= dom_id(comment) %> class="comment">
   <em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
   <%= link_to "Remove", [@commentable, comment], :method => :delete, :remote => true %>
   <%= simple_format comment.content %>
 </div>

馈送/ show.html.erb

 <div id="comments">
    <%= render @comments %>
 </div>

评论控制器

 class CommentsController < ApplicationController

   def create
     @comment = @commentable.comments.new(params[:comment])
     if @comment.save
        respond_to do |format|
           format.html { redirect_to @commentable }
           format.js
        end
     else
        render :new
     end
    end

    def destroy
      @comment = Comment.find(params[:id])
      @commentable = @comment.commentable
        if @comment.destroy
          respond_to do |format|
            format.html { redirect_to @commentable }
            format.js
          end
        end
     end
  end

提供控制器

 class FeedsController < ApplicationController
  def show
     @feed = Feed.find(params[:id])
     @commentable = @feed
     @comments = @commentable.comments
     @comment = Comment.new
   end
 end

1 个答案:

答案 0 :(得分:0)

从它的外观来看,@ tell会在你到达destroy.js.erb时包含一个注释,因为你在之前的控制器中设置它,所以我不认为它与引用的答案有任何关系。我想知道这是否是由于你的id周围缺少引号引起的:<div id=<%= dom_id(comment) %> class="comment"> ...我怀疑如果你纠正这个,以及任何其他相关的HTML验证错误,它将开始工作。

相关问题