title: Ruby/Rails/AJAX/JQuery - On link click, passing a parameter and performing a controller action
body: <p>I'm trying to use clicking a link to perform and action in my controller called 'yes' but do so client side rather than having to refresh everytime a user clicks.</p>
  
  <p>Before I had an link_to that routed to a action called "yes" and passed the id of a model I have called 'event'</p>
  
  <pre><code>      &lt;%= link_to "yes", yes_path(event)%&gt;   (in view)
  
        match 'user/:id/yes' =&gt; 'user#yes', :as =&gt; "yes"   {in routes.rb)
  </code></pre>
  
  <p>The problem issue is that every time the user clicks the link the page refreshes while it performs the yes action, so it will flow alot smoother if I can tell the backend to perform the actions client side.</p>
  
  <p>S0  I found a reference here :   <a href="https://stackoverflow.com/questions/4754571/execute-controller-action-from-javascript-in-rails3">execute controller/action from javascript in rails3</a></p>
  
  <p>and took a look at the documentation :  <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow noreferrer">http://api.jquery.com/jQuery.ajax/</a></p>
  
  <p>And came up with this.  Where if the post is successful at the previous route from above change a css class for the link (change color).</p>
  
  <pre><code> $.ajax({
         type: "POST",
         url: "/user/" + $(this).attr('event') + "/yes/",
         success: function(){
             $(".like").click(function() {
             if ($(this).hasClass("selected")) {
             $(this).addClass("selected");
             return false;  } 
      });
  </code></pre>
  
  <p>I also added this is the bottom of the controller that the desired javascript is being used.</p>
  
  <pre><code>respond_to do |format|
       format.html { }
       format.js
  end
  </code></pre>
  
  <p>So now my link_to looks like this</p>
  
  <pre><code> &lt;%= link_to "yes", yes_path(event), :class =&gt; "like", :remote =&gt; true %&gt;
  </code></pre>
  
  <p>But the page is still refreshing and It doesnt look like its calling the AJAX at all.</p>
  
  <ul>
  <li>am I passing the parameter "event" properly as a jquery attribute?</li>
  <li>am I calling the link_to properly?
  This is my first time so I have no idea what I'm doing wrong, possibly a few things?</li>
  </ul>
  
  <p>I'd really appreciate any help</p>