Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX Request is hiding text rather than placing the partial

I'm trying to implement replies with comments and so when you click "reply" a partial should show up underneath the current comment.

So in my reply.js.erb, I have

$("<%= j render(:partial => 'reply', :locals => { :comment => Comment.build_from(@obj, current_user.id, "") }) %>").insertAfter($('<%= @div_id %>')).show('fast');

Where @div_id is the division id for the comment that it's replying to. So what's happening right now is that the partial will not display and it's also hiding the content underneath the @div_id. Not sure what's going on.

EDIT: So, I believe I figured out why it's being hidden. I have another javascript file in assets called comments.js.coffee which contains this -

jQuery ->
  $(document)
    .on "ajax:beforeSend", ".comment", ->
      $(this).fadeTo('fast', 0.5)
    .on "ajax:success", ".comment", ->
      debugger;
      $(this).hide('fast')
    .on "ajax:error", ".comment", ->
      debugger;
      $(this).fadeTo('fast', 1)

".comment" is the header for the partial which contains the Reply link. That same partial contains a Destroy link. So somehow when I click Reply it's running this code as well which is hiding the comment afterwards. Here's my partial for comment

%div.comment{ :id => "comment-#{comment.id}" }
    %hr
    = link_to "×", comment_path(comment), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this comment?", :disable_with => "×", :class => 'close'
    %h4
        = comment.user.first_name
        %small= comment.updated_at
    %p= comment.body
    %p= link_to "Reply", comment_reply_path(comment), :method => :get, :remote => true

How would I go about fixing this?

like image 306
Jill Avatar asked Aug 20 '15 09:08

Jill


3 Answers

May be @div_id is not having # with it, so below code may work after prepending # before @div_id

$("<%= j render(:partial => 'reply', :locals => { :comment => Comment.build_from(@obj, current_user.id, "") }) %>").insertAfter($('#<%= @div_id %>')).show('fast');
like image 94
Vishnu Atrai Avatar answered Nov 16 '22 02:11

Vishnu Atrai


Can you try the following?

$('<%= @div_id %>').append("<%= j render(:partial => 'reply', :locals => { :comment => Comment.build_from(@obj, current_user.id, "") }) %>").show('fast');

Updated:

can you try append instead?

like image 34
Jay-Ar Polidario Avatar answered Nov 16 '22 01:11

Jay-Ar Polidario


$("<%= j ( render partial: 'reply', :locals => { :comment => Comment.build_from(@obj, current_user.id, "") }) %>").insertAfter($('#<%= @div_id %>')).show('fast');

This will work correctly....just syntactical change

like image 2
Gaurav Mamidwar Avatar answered Nov 16 '22 00:11

Gaurav Mamidwar