NoMethodError with a twist

Usual disclaimers are in order that I’m pretty new to RoR and don’t know
exactly how to approach debugging this problem, but I’ve looked over
about 20 Google pages+these forums and can’t figure this one out…so
hoping someone very smart can give me an idea here.

I’m trying to make an AJAX call to a method (“new_item_add_image”) in a
file called booths_controller.rb using the following
javascript-generated form:

— Begin —
var form = document.createElement(‘form’);
form.setAttribute(‘id’, id);
form.setAttribute(‘target’, frameID);
form.setAttribute(‘method’, ‘post’);
form.setAttribute(‘enctype’, ‘multipart/form-data’);
form.setAttribute(‘action’, ‘/booths/new_item_add_image’);
document.body.appendChild(form);
… A couple input elements are Javascript-generated here…
— End —

Submitting this form usually gives me the following error:

— Begin —
Status: 500 Internal Server Error
Content-Type: text/html
Content-Length: 60

500 Internal Server Error

# ["c:/ruby/lib/ruby/1.8/webrick/httputils.rb:129:in `parse_header'", --- End ---

I say “usually” because there are two circumstances in which the form
calls the “new_item_add_image” method properly: 1) if put a call to
ruby-debug (e.g., “debugger”) anywhere in the method and step through
it, it acts fine or 2) if I clear my cache and cookies, the form will
usually submit fine the first try and err on subsequent attempts.

I can not make rhyme nor reason of what could be going wrong here, in
particular why adding the debugger line and stepping through the
function causes it to work. Here’s some of the method in question, if
it matters:

— Begin —
def new_item_add_image
#debugger # if commented, the method isn’t called and the error is
given
flash[:notice] = “We are arrived. Woohoo!” # Shows this message if
the debugger line is uncommented. Does not show otherwise.

image_id = (params[:image_id] =~ /\_(\d+)$/ ? $1.to_i : 0);

if params[:image]
  if session[:new_item_image] && session[:new_item_image][image_id]
    session[:new_item_image][image_id].destroy
    session[:new_item_image][image_id] = nil
  end

  session[:new_item_image] ||= Array.new
  session[:new_item_image][image_id] = Image.new(params[:image])

  if session[:new_item_image][image_id].valid?
    session[:new_item_image][image_id].save
    active_booth.images[image_id] =

session[:new_item_image][image_id]
end
end

render :text => %(/*-secure-\n{ "image_main" :

“#{session[:new_item_image][image_id].public_path}” }\n*/)
end

— End —

Thanks much for any ideas at all!

Bill