title: How do you call a controller action with jQuery from your application.js file?
body: <p>I'm using the jQuery autocomplete plugin, and I want to customize this event:</p>
  
  <pre><code>select: function(event, ui) {
             $('.topic_field').val(ui.item.topic.name);
             return false;
  </code></pre>
  
  <p>Essentially, it triggers callbacks when an element from the dropdown list is selected. As of now, it only adds the selected element to the text field. I want both the field to be populated and for my application to send a POST request to the video update controller action, so that the user does not need to explicitly press the button. How can I do this?</p>
  
  <p>UPDATE:</p>
  
  <p>Here is the form in the show view of the video controller:</p>
  
  <pre><code>&lt;%= form_for @video, :url =&gt; {:action =&gt; "update"}, :remote =&gt; true do |f| %&gt;
    &lt;div class="field"&gt;
    &lt;%= f.text_field :topic_names, :class =&gt; "topic_field" %&gt;
    &lt;/div&gt;
    &lt;%= f.submit "Add Topic" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Here is my jQuery code:</p>
  
  <pre><code>var url = $('.edit_video').attr('action');
  var val = ui.item.topic.name;
  $.post(url, {data:val});
  </code></pre>
  
  <p>This is in my routes.rb:</p>
  
  <pre><code>resources :videos
  resources :video_votes
  resources :users
  resources :profiles
  resources :genres
  resources :topics
  resources :topicables
  resource :session
  </code></pre>
  
  <p>Here's my update action:</p>
  
  <pre><code>def update
  @video = current_user.videos.find(params[:id])
  
  respond_to do |format|
    if @video.update_attributes(params[:video])
      format.html { redirect_to(@video) }
      format.js
    else
      format.html { render :action =&gt; "edit" }
    end
  end
  end
  </code></pre>