title: Rails: AJAX Controller JS not firing
body: <p>I'm having an issue with one of my controller's AJAX functionality.  Here's what I have:</p>
  
  <pre><code>class PhotosController &lt; ApplicationController
    # ...
    def create
      @photo = Photo.new(params[:photo])
      @photo.image_content_type = MIME::Types.type_for(@photo.image_file_name).to_s
      @photo.image_width = Paperclip::Geometry.from_file(params[:photo][:image]).width.to_i
      @photo.image_height = Paperclip::Geometry.from_file(params[:photo][:image]).height.to_i
      @photo.save!
  
      respond_to do |format|
        format.js
      end
    end
    # ...
  end
  </code></pre>
  
  <p>This is called through a <code>POST</code> request sent by this code:</p>
  
  <pre><code>$(function() {
    // add photos link
    $('a.add-photos-link').colorbox({
      overlayClose: false,
      onComplete: function() { wire_add_photo_modal(); }
    });
  
    function wire_add_photo_modal() {
      &lt;% session_key = ActionController::Base.session_options[:key] %&gt;
      $('#upload_photo').uploadify({
        uploader: '/swf/uploadify.swf',
        script: '/photos',
        cancelImg: '/images/buttons/cancel.png',
        buttonText: 'Upload Photo(s)',
        auto: true,
        queueID: 'queue',
        fileDataName: 'photo[image]',
        scriptData: {
          '&lt;%= session_key %&gt;': '&lt;%= u cookies[session_key] %&gt;',
          commit: 'Adding Photo',
          controller: 'photos',
          action: 'create',
          '_method': 'post',
          'photo[gallery_id]': $('#gallery_id').val(),
          'photo[user_id]': $('#user_id').val(),
          authenticity_token: encodeURIComponent('&lt;%= u form_authenticity_token if protect_against_forgery? %&gt;')
        },
        multi: true
      });
    }
  });
  </code></pre>
  
  <p>Finally, I have my response code in <code>app/views/photos/create.js.erb</code>:</p>
  
  <pre><code>alert('photo added!');
  </code></pre>
  
  <p>My log file shows that the request was successful (the photo was successfully uploaded), and it even says that it rendered the <code>create</code> action, yet I never get the alert.  My browser shows NO javascript errors.</p>
  
  <p>Here's the log AFTER a request from the above <code>POST</code> request is submitted:</p>
  
  <pre><code>Processing PhotosController#create (for 127.0.0.1 at 2010-03-16 14:35:33) [POST]
  Parameters: {"Filename"=&gt;"tumblr_kx74k06IuI1qzt6cxo1_400.jpg", "photo"=&gt;{"user_id"=&gt;"1", "image"=&gt;#&lt;File:/tmp/RackMultipart20100316-54303-7r2npu-0&gt;}, "commit"=&gt;"Adding Photo", "_edited_session"=&gt;"edited", "folder"=&gt;"/kakagiloon/", "authenticity_token"=&gt;"edited", "action"=&gt;"create", "_method"=&gt;"post", "Upload"=&gt;"Submit Query", "controller"=&gt;"photos"}
  [paperclip] Saving attachments.
  [paperclip] saving /public/images/assets/kakagiloon/thumbnail/tumblr_kx74k06IuI1qzt6cxo1_400.jpg
  [paperclip] saving /public/images/assets/kakagiloon/profile/tumblr_kx74k06IuI1qzt6cxo1_400.jpg
  [paperclip] saving /public/images/assets/kakagiloon/original/tumblr_kx74k06IuI1qzt6cxo1_400.jpg
  Rendering photos/create
  Completed in 248ms (View: 1, DB: 6) | 200 OK [http://edited.local/photos]
  </code></pre>
  
  <p>NOTE: I edited out all the SQL statements and I put "edited" in place of sensitive info.</p>
  
  <p>What gives? Why aren't I getting my <code>alert();</code>?</p>
  
  <p>Please let me know if you need anymore info to help me solve this issue! Thanks.</p>
  
  <hr>
  
  <p>SOLUTION:  Thanks to jitter for setting me straight about Uploadify's callbacks.</p>
  
  <p>Controller:</p>
  
  <pre><code>class PhotosController &lt; ApplicationController
    # ...
    def create
      @photo = Photo.new(params[:photo])
      @photo.image_content_type = MIME::Types.type_for(@photo.image_file_name).to_s
      @photo.image_width = Paperclip::Geometry.from_file(params[:photo][:image]).width.to_i
      @photo.image_height = Paperclip::Geometry.from_file(params[:photo][:image]).height.to_i
      @photo.save!
  
      respond_to do |format|
        format.js { render :layout =&gt; false }
      end
    end
    # ...
  end
  </code></pre>
  
  <p>Javascript (Uploadify) callback:</p>
  
  <pre><code>$(function() {
    // add photos link
    $('a.add-photos-link').colorbox({
      overlayClose: false,
      onComplete: function() { wire_add_photo_modal($(this).parent()); }
    });
  
    function wire_add_photo_modal($parent) {
      &lt;% session_key = ActionController::Base.session_options[:key] %&gt;
      $('#upload_photo').uploadify({
        uploader: '/swf/uploadify.swf',
        script: '/photos',
        cancelImg: '/images/buttons/cancel.png',
        buttonText: 'Upload Photo(s)',
        auto: true,
        queueID: 'queue',
        fileDataName: 'photo[image]',
        scriptData: {
          '&lt;%= session_key %&gt;': '&lt;%= u cookies[session_key] %&gt;',
          commit: 'Adding Photo',
          controller: 'photos',
          action: 'create',
          '_method': 'post',
          'photo[gallery_id]': $('#gallery_id').val(),
          'photo[user_id]': $('#user_id').val(),
          authenticity_token: encodeURIComponent('&lt;%= u form_authenticity_token if protect_against_forgery? %&gt;')
        },
        multi: true,
        onComplete: function(event, queueID, fileObj, response, data) { 
          $parent.find('.gallery-photos').append(response);
        }
      });
    }
  });
  </code></pre>
  
  <p>In <code>create.js.haml</code> (changed templating language from ERB, since I'm basically just returning HTML content):</p>
  
  <pre><code>- if @photo.gallery.nil?
    =render :partial =&gt; 'photos/photo', :locals =&gt; { :photo =&gt; @photo, :rel =&gt; 'unsorted' }
  - else
    = render :partial =&gt; 'photos/photo', :locals =&gt; { :photo =&gt; @photo, :rel =&gt; gallery.title }
  </code></pre>