title: Ajax:success not working in rails app
body: <p>Within a rails 4 app, I am using a link_to to send an upvote on posts via json.</p>
  
  <p>Here is what I have in my posts controller:</p>
  
  <pre><code>def upvote
    @post = Post.find(params[:id])
    @post.liked_by current_user
  
      respond_to do |format|
      format.html {redirect_to :back }
      format.json { render json: { count: @post.get_upvotes.size } }
    end
  end
  </code></pre>
  
  <p>Here is what I have in my view</p>
  
  <pre><code>&lt;%= link_to like_post_path(post), method: :put, class: 'vote', remote: true, data: { type: :json } do %&gt;
      &lt;%= image_tag('vote.png') %&gt;
      &lt;%= content_tag :span, post.get_upvotes.size %&gt;
      &lt;% end %&gt;
  
  
      &lt;script&gt;
  
      $(document)
          .on('ajax:send', '.vote', function () { $(this).addClass('loading'); })
          .on('ajax:complete', '.vote', function () { $(this).removeClass('loading'); })
          .on('ajax:error', '.vote', function(e, xhr, status, error) { console.log(status); console.log(error); })
          .on('ajax:success', '.vote', function (e, data, status, xhr) {
          $(this).find("span").html(data.count);
          $(this).find("img").attr("src", '&lt;%= asset_path 'voted.png' %&gt;'); 
          });
  
  
      &lt;/script&gt;
  </code></pre>
  
  <p>When I click on the link, the vote goes through as a JSON request, I see this in my log:</p>
  
  <pre><code>Processing by PostsController#upvote as JSON
  </code></pre>
  
  <p>But for some reason, my snipped of javascript is not working. Neither the counter or the icon update. <strong>How can I fix this? Does this have to do with turbolinks, does it have to do with where I am placing the javascript?</strong></p>