0

我正在实现 'acts_as_commentable_with_threading' 并且 js 脚本不适用于删除或创建操作。我必须重新加载页面才能在页面中添加和删除我的评论。

在此处输入图像描述

我已将“comments.js.coffee”文件放在“app/assets/javascript”目录中。它如下所示:

#comments.js.coffee

jQuery ->
# Create a comment
  $(".comment-form")
    .on "ajax:beforeSend", (evt, xhr, settings) ->
      $(this).find('textarea')
        .addClass('uneditable-input')
        .attr('disabled', 'disabled');
    .on "ajax:success", (evt, data, status, xhr) ->
      $(this).find('textarea')
        .removeClass('uneditable-input')
        .removeAttr('disabled', 'disabled')
        .val('');
      $(xhr.responseText).hide().insertAfter($(this)).show('slow')

# Delete a comment
  $(document)
    .on "ajax:beforeSend", ".comment", ->
      $(this).fadeTo('fast', 0.5)
    .on "ajax:success", ".comment", ->
      $(this).hide('fast')
    .on "ajax:error", ".comment", ->
      $(this).fadeTo('fast', 1)

  #In Comments controller file I wrote:

def create
    @comment_hash = params[:comment]
    @obj =  @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
    # Not implemented: check to see whether the user has permission to create a comment on this object
    @comment = Comment.build_from(@obj, current_user, @comment_hash[:body])
    if @comment.save
      render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created
    else
      render :js => "alert('error saving comment');"
    end
  end

def destroy
  @comment = Comment.find(params[:id])
  if @comment.destroy
    render :json => @comment, :status => :ok
  else
    render :js => "alert('error deleting comment');"
  end
  end
  end

# In Catalogs controller the code is:

def find_user
@member = Member.find(params[:id])
end


def show_user
find_user  
@comments = @member.comment_threads.order('created_at desc')
@new_comment = Comment.build_from(@member, @member.id, "","")
respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @member }

end

end

#In Comments model, the code include:

acts_as_nested_set :scope => [:commentable_id, :commentable_type]
attr_accessible :commentable, :body,:member_id,:sender
validates :body, :presence => true,:length=>{:maximum=>1000}
validates :member, :presence => true,:length=>{:maximum=>200}
validates :sender,:presence=> true,:length=>{:maximum=>200} 

belongs_to :commentable, :polymorphic => true

# NOTE: Comments belong to a user
belongs_to :member


def self.build_from(obj, member_id, comment,sender)
new \
  :commentable => obj,
  :member_id     => member_id,
  :body        => comment,
  :sender      => sender
 end
...................

# In Member model, I add the following lines:
acts_as_commentable

has_many :comments, :dependent=>:destroy

# In the view file 'show_user', I wrote:

<div class='comments'>
<%= render :partial => 'comments/form', :locals => { :comment => @new_comment }%> 
<%= render :partial => 'comments/comment', :collection => @comments, :as => :comment %> 
   </div>
#Partial 'comments/comment' contains:

<div class='comment'>
 <hr>
 <b><%= comment.sender %> </b>
 <small><%= comment.updated_at%></small> <%= link_to "×",comment_path(comment), :method =>   :delete, :remote => true, :confirm => "Are you sure you want to remove this comment from #  {comment.sender}?", :disable_with => "×", :class => 'close'%>
  <p><%= comment.body.html_safe%></p>
  </div>
# In _form.html.erb file:
<div class='comment-form'>

<%= simple_form_for comment, :remote => true do |f|%>
<small>Leave your comment </small><br/> 
<%= f.input :sender,:as => :hidden,:input_html => { :rows => "2", :value =>"# {current_user.login}"}, :label => false %>
<%= f.input :body, :input_html => { :rows => "2", :class =>"tinymce"}, :label => false %>



<%= f.input :commentable_id, :as => :hidden, :value => comment.commentable_id %>
    <%= f.input :commentable_type, :as => :hidden, :value => comment.commentable_type %>


 <%= f.button :submit, :class => "btn btn-primary", :disable_with => "Submitting…" %>
 <%end%> 
  </div>

在配置/路由文件中:

resources :comments, :only => [:create, :destroy]

#Here is the output I get if I click on to 'create comments' button:

在此处输入图像描述

我希望有人能帮助我。谢谢你。

4

1 回答 1

0
I **deleted** the jquery.min.js file from my app/assets/javascripts directory
and in my gem file I added the line : gem 'jquery-rails' 
and used 'bundle install'  to use the gem in my app.
The application.js file has three lines:

//= require jquery
//= require jquery_ujs
//= require_tree .

And in my layout.html.erb file I have the line:

<%= javascript_include_tag  'application' %>

I know it is pretty obvious. Sometimes you need to get things wrong 
to understand the obvious better. Now I can add and remove 
messages using AJAX and js files.

在此处输入图像描述

于 2013-09-07T07:37:56.947 回答