Jaqx
Jaqx

Reputation: 826

Why am I getting two dialog box confirmations (rails)

When I click the link to delete a comment I receive two dialog box confirmations. When I remove the confirmation I don't get any.

view/comments/_comment:

<span class="comment_info">
  <aside class="span2">
    <%= link_to gravatar_for((comment.user), size: 35), comment.user %>
    <%= link_to comment.user.name, comment.user, id: "feedusername" %>
  </aside>
<span id="comment_contentz"> <%= comment.content %>
<span id="timestamp_and_delete">
  Said <%= time_ago_in_words(comment.created_at) %> ago.
  <% if current_user?(comment.user) || current_user.id == (comment.micropost.user_id) %>
    <%= link_to "erase", comment, method: :delete, id:"",
            data: { confirm: "You sure?" },
            title: comment.content %>
    <% end %>
  </span>
  </span>
</span>

I have the same problem with my posts

Microposts/_micropost:

  <span class="timestamp">Posted  
    <%=time_ago_in_words(micropost.created_at) %> ago.</span>
    <%= render 'shared/delete_micropost', object: micropost %> </span>
    <br/>
    <%= micropost.content %>
  </span>
<%= render micropost.comments %>

shared/delete_micropost:

<% if current_user?(object.user) %>
  <%= link_to "delete", object, method: :delete,        id:"micropost_delete",
            data: { confirm: "Remove Post?" },
            title: object.content %>
<% end %>

Upvotes: 1

Views: 326

Answers (1)

Anuj Biyani
Anuj Biyani

Reputation: 329

This usually happens when a click handler is bound twice to the same element. Check that your javascript files aren't included twice.

If you're on Rails 3.2.8, look for multiple includes of application.js or jquery_ujs. Alternatively you can try setting a breakpoint using the Javascript console on the click handler that is called when you click on the delete button. You'll have to dig for that method somewhere in the aforementioned files.

If that breakpoint gets hit twice, then you probably have included that file twice. If it gets hit only once, then some other code is setting a click handler on the same element.

Caveat: like 244an said in his comment on your post, you don't have to put the confirm: "You sure? inside of a data hash. You can just do this: <%= link_to 'erase', delete_path, confirm: 'You sure?' %>. See UrlHelper#link_to. Doesn't seem to hurt anything, though, to do it the way you did.

Upvotes: 2

Related Questions