javascript - JS script is not working on create and delete actions -
i implementing 'acts_as_commentable_with_threading' , js script not working delete or create actions. have reload page comments added , removed page.
i have placed 'comments.js.coffee' file in 'app/assets/javascript' directory. looks following:
#comments.js.coffee jquery -> # create 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 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 wrote: def create @comment_hash = params[:comment] @obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id]) # not implemented: check see whether user has permission create comment on 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 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 |format| format.html # show.html.erb format.json { render json: @member } end end #in comments model, 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 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, add following lines: acts_as_commentable has_many :comments, :dependent=>:destroy # in view file 'show_user', 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 sure want remove comment # {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 |f|%> <small>leave 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>
in config/routes file:
resources :comments, :only => [:create, :destroy] #here output if click on 'create comments' button:
i hope me. thank you.
i **deleted** jquery.min.js file app/assets/javascripts directory , in gem file added line : gem 'jquery-rails' , used 'bundle install' use gem in app. application.js file has 3 lines: //= require jquery //= require jquery_ujs //= require_tree . , in layout.html.erb file have line: <%= javascript_include_tag 'application' %> know pretty obvious. need things wrong understand obvious better. can add , remove messages using ajax , js files.
Comments
Post a Comment