Ajax with Polymorphic Association(Destroy& Create)

时间:2013-08-04 22:33:59

标签: ruby-on-rails ruby ajax jquery polymorphic-associations

我与dailyposts& amp;我的rails应用程序中的评论

数据库中的一切正常,但是当我尝试将Ajax添加到Destroy Action时(我使用了本教程http://railscasts.com/episodes/136-jquery-ajax-revised) ....除非我刷新页面,否则它不起作用。 (但我的警报在destroy.js.erb中有效)

我知道我的错误在destroy.js.erb

Rails的新手......请帮助:)

这是我的代码......

路线

resources :dailyposts do
  resources :comments
end

控制器

##Dailyposts

class DailypostsController < ApplicationController

  respond_to :html, :js

  def show
    @user = User.find_by_username(params[:username])
    @dailypost = Dailypost.find_by_id(params[:id])
    @commentable = @dailypost
    @comments = @commentable.comments.arrange(:order => :created_at)
    @comment = Comment.new
  end
end

##Comments

class CommentsController < ApplicationController
  before_filter :load_commentable

  respond_to :html, :js

  def create
    @comment = @commentable.comments.create(params[:comment])
    @comment.user = current_user
    if @comment.save
      respond_to do |format|
        format.html { redirect_to @commentable }
        format.js
      end
    else
      redirect_to @commentable, notice: "Comment can't be blank."
    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

  private 

  def load_commentable
    resource, id = request.path.split('/')[1, 2]
    @commentable = resource.singularize.classify.constantize.find(id)
  end

end

视图

Dailypost show.html.erb

  <div class="row-fluid">
    <div class="span12">

      <div>
        <%= raw(dailypost_with_links(@dailypost)) %>
      </div>

      <%= render "comments/form" %>

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

    </div>

  </div>

_comment.html.erb

<section class="comments">
  <div class ="user">
    <%= link_to comment.user.username, comment.user  %> 
    <%= comment.content %>
  </div>

##Here I am passing remote: true for ajax

    <% if current_user?(comment.user) %> 
      <%= link_to content_tag(:i, "", class: "icon-trash icons"), [@commentable, comment], method: :delete,
                       data: { confirm: "Are you sure?" },
                       title: "Delete", remote: true %> |
    <% end %>

</section>

destroy.js.erb

##alert is working
alert('ajax works!');

$('#<%= dom_id(@comment) %>').remove();

日志

Started DELETE "/dailyposts/11/comments/133" for 127.0.0.1 at 2013-08-04 23:06:31 -0700
Processing by CommentsController#destroy as JS
  Parameters: {"dailypost_id"=>"11", "id"=>"133"}
  Dailypost Load (0.3ms)  SELECT "dailyposts".* FROM "dailyposts" WHERE "dailyposts"."id" = ? ORDER BY dailyposts.created_at DESC LIMIT 1  [["id", "11"]]
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'sgFH2XeZWEXCcjxiAwgfXg' LIMIT 1
  Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1  [["id", "133"]]
  Dailypost Load (0.1ms)  SELECT "dailyposts".* FROM "dailyposts" WHERE "dailyposts"."id" = 11 ORDER BY dailyposts.created_at DESC LIMIT 1
   (0.1ms)  begin transaction
  Comment Load (0.1ms)  SELECT "comments".* FROM "comments" WHERE (comments.ancestry like '133/%' or comments.ancestry = '133')
  SQL (0.4ms)  DELETE FROM "comments" WHERE "comments"."id" = ?  [["id", 133]]
   (2.3ms)  commit transaction
  Rendered comments/destroy.js.erb (0.7ms)
Completed 200 OK in 25ms (Views: 10.3ms | ActiveRecord: 3.9ms)

3 个答案:

答案 0 :(得分:1)

重定向正在打破响应。尝试将其更改为redirect_to( :back ) unless request.xhr?

答案 1 :(得分:1)

试试这个:

Dailypost show.html.erb

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

<强> _comment.html.erb

 <div id="<%= dom_id(comment) %>"> 
   <%= comment.content %>
 </div>

<强> create.js.erb

 $('<%= escape_javascript(render(:partial => @comment))%>').appendTo('#comments').hide().fadeIn();

<强> destroy.js.erb

 $('#<%= dom_id(@comment) %>').remove();

答案 2 :(得分:0)

问题出在destroy.js.erb,第二行不起作用,因为没有像edit_comment _(:id)格式的 edit_comment_1 这样的ID。

_comment.html.erb中,添加一个包含.js.erb

中使用的ID的相应div
<section class="comment">

 <div id="edit_comment_<%= comment.id %>">

  <div class ="user">
     <%= link_to comment.user.username, comment.user  %> 
     <%= comment.content %>
   </div>

    <% if current_user?(comment.user) %> 
      <%= link_to content_tag(:i, "", class: "icon-trash icons"), [@commentable, comment], method: :delete,
                       data: { confirm: "Are you sure?" },
                       title: "Delete", remote: true %> |
    <% end %>

 </div>
</section>