Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 691729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:34:32+00:00 2026-05-14T02:34:32+00:00

I’m having an issue with one of my controller’s AJAX functionality. Here’s what I

  • 0

I’m having an issue with one of my controller’s AJAX functionality. Here’s what I have:

class PhotosController < 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

This is called through a POST request sent by this code:

$(function() {
  // add photos link
  $('a.add-photos-link').colorbox({
    overlayClose: false,
    onComplete: function() { wire_add_photo_modal(); }
  });

  function wire_add_photo_modal() {
    <% session_key = ActionController::Base.session_options[:key] %>
    $('#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: {
        '<%= session_key %>': '<%= u cookies[session_key] %>',
        commit: 'Adding Photo',
        controller: 'photos',
        action: 'create',
        '_method': 'post',
        'photo[gallery_id]': $('#gallery_id').val(),
        'photo[user_id]': $('#user_id').val(),
        authenticity_token: encodeURIComponent('<%= u form_authenticity_token if protect_against_forgery? %>')
      },
      multi: true
    });
  }
});

Finally, I have my response code in app/views/photos/create.js.erb:

alert('photo added!');

My log file shows that the request was successful (the photo was successfully uploaded), and it even says that it rendered the create action, yet I never get the alert. My browser shows NO javascript errors.

Here’s the log AFTER a request from the above POST request is submitted:

Processing PhotosController#create (for 127.0.0.1 at 2010-03-16 14:35:33) [POST]
Parameters: {"Filename"=>"tumblr_kx74k06IuI1qzt6cxo1_400.jpg", "photo"=>{"user_id"=>"1", "image"=>#<File:/tmp/RackMultipart20100316-54303-7r2npu-0>}, "commit"=>"Adding Photo", "_edited_session"=>"edited", "folder"=>"/kakagiloon/", "authenticity_token"=>"edited", "action"=>"create", "_method"=>"post", "Upload"=>"Submit Query", "controller"=>"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]

NOTE: I edited out all the SQL statements and I put “edited” in place of sensitive info.

What gives? Why aren’t I getting my alert();?

Please let me know if you need anymore info to help me solve this issue! Thanks.


SOLUTION: Thanks to jitter for setting me straight about Uploadify’s callbacks.

Controller:

class PhotosController < 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 => false }
    end
  end
  # ...
end

Javascript (Uploadify) callback:

$(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) {
    <% session_key = ActionController::Base.session_options[:key] %>
    $('#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: {
        '<%= session_key %>': '<%= u cookies[session_key] %>',
        commit: 'Adding Photo',
        controller: 'photos',
        action: 'create',
        '_method': 'post',
        'photo[gallery_id]': $('#gallery_id').val(),
        'photo[user_id]': $('#user_id').val(),
        authenticity_token: encodeURIComponent('<%= u form_authenticity_token if protect_against_forgery? %>')
      },
      multi: true,
      onComplete: function(event, queueID, fileObj, response, data) { 
        $parent.find('.gallery-photos').append(response);
      }
    });
  }
});

In create.js.haml (changed templating language from ERB, since I’m basically just returning HTML content):

- if @photo.gallery.nil?
  =render :partial => 'photos/photo', :locals => { :photo => @photo, :rel => 'unsorted' }
- else
  = render :partial => 'photos/photo', :locals => { :photo => @photo, :rel => gallery.title }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:34:33+00:00Added an answer on May 14, 2026 at 2:34 am

    I can’t see that you use any of uploadify’s callbacks. Thus I guess it just ignores whatever the server sends as response. Try wiring up the onComplete option with a function if you want to send a meaningful response back to your javascript

    Check the uploadify documentation for more

    I imagine you could change app/views/photos/create.js.erb to

    photo added!
    

    and then do

    $('#upload_photo').uploadify({
        ...
        onComplete: function(event, queueID, fileObj, response, data) {
            alert("Server said: "+response+" for file "+fileObj.name);
        }
        ...
    });
    

    If you want to add the photo to the page you can just let the server return the appropriate html snippet e.g. I imagine create.js.erb to create/output something like this

    <img src="Xxx.jpg" width="..." height="..." />
    

    All you need to do in the callback is

    onComplete: function(event, queueID, fileObj, response, data) {
        $("selectorforwheretheimageshouldbeinserted").append(response);
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.