16

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?

3
  • Can you please post the HTML before and after the request? Aug 20, 2015 at 9:34
  • How do I do that? Thanks
    – Jill
    Aug 20, 2015 at 9:39
  • IIRC this is the 10 millionth question on Stack Overflow
    – 10 Rep
    Apr 20, 2021 at 0:19

3 Answers 3

6

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');
3
  • The partial shows but what's in @div_id gets hidden.
    – Jill
    Aug 20, 2015 at 10:01
  • Try this code - $('#<%= @div_id %>').html("<%= j render(:partial => 'reply', :locals => { :comment => Comment.build_from(@obj, current_user.id, '') }) %>").fadeIn('fast'); Aug 20, 2015 at 10:07
  • I figured out the reason why it's hiding but I do not know how to fix it. I updated the question.
    – Jill
    Aug 20, 2015 at 10:17
3

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?

3
  • That does the same. Partial doesn't show up and it hides what's in @div_id
    – Jill
    Aug 20, 2015 at 9:59
  • @Jill, can you try append instead? Aug 20, 2015 at 10:02
  • I figured out the reason why it's hiding but I do not know how to fix it. I updated the question.
    – Jill
    Aug 20, 2015 at 10:16
2
$("<%= 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

3
  • Your updated answer renders the partial correctly however it still hides whatever is found within @div_id
    – Jill
    Aug 20, 2015 at 9:58
  • try this $('<%= @div_id %>').insertAfter('HELLO '); let me know if this works Aug 20, 2015 at 10:05
  • I figured out the reason why it's hiding but I do not know how to fix it. I updated the question.
    – Jill
    Aug 20, 2015 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.