title: How to use Rails respond_with with a create action and javascript?
body: <p>I've been reading about respond_with. and used it before in a couple of tutorials but dont really think I understand it fully.</p>
  
  <p>The functionality I am trying to implement right now  is this: I have a list of items of class Article, and each of them has a link to the create Favorite action. When the user clicks on it  it the favorite instance is created and the user is redirected to the home page. I want this to work with AJAX without a page refresh, and execute some JavaScript on the article item to let the user know it's been favourited successfully. I've used the :remote => true attribute in the relevant link, so the action is executing remotely without problem, but I am a bit stuck on how to execute the action's .js.erb file.</p>
  
  <p>I've done the following in the controller:</p>
  
  <pre><code>class FavouritesController &lt; ApplicationController
  before_filter :authenticate
  respond_to :html, :js
  
  
    def create
      @article = Article.find_by_id(params[:article_id])
      current_user.Favourites.create(:article =&gt; @article)
      redirect_to root_path 
    end
  </code></pre>
  
  <p>The problem is, I dont know what parameter should i pass to respond_with. I've tried replacing the redirect with both respond_with @article, and respond_with without parameters, and while both of them work (the create.js.erb is called), I dont understand why...</p>
  
  <p>I dont want to 'respond_with' anything, but only that when the action is executed via javascript, the create.js.erb gets called. Can anyone explain a) why does respond_with work anyway whatever I pass to it, and b) what is the right way to do this?</p>
  
  <p>BTW, I am using Rails 3.0</p>
  
  <p>EDIT:  I understand it better if I use respond_to like in the following code, but I would like to understand respond_with better.</p>
  
  <pre><code>respond_to do |format|
    format.html { redirect_to root_path }
    format.js
  end
  </code></pre>