Help! I am so close.
Here's the thing, I am committed to ding it this way for a number of reasons. I have inherited a codebase that was done pretty well, and I can't really be reinventing the wheel over and over.
What we have here is a relatively simple image upload partial, that is attached to a separate edit form. The whole thing works fine, except for a strange javascript error that I only see in Firebug. It says this:
GET http://localhost:3000/highlights/73/highlight_images/new (754ms)prototype.js (line 1223) loc is not defined [Break on this error] <title>Action Controller: Exception caught</
create_image (line 3)
ok stay with me here - I configured the highlight_image as a sub- resource of highlights. I found out that uploads cannot happen remotely, so that is why this is one of the only straight upload utilities in the entire app. When I leave out the 'responds to parent' call at the bottom of the controller method, everything is fine but I end up with a modal overlay and a spinning upload widget (ie, create_image.rjs does not run). The upload does run correctly, however.
now, if i put the responds to parent call back in, the rjs runs, the modal overlay goes away, the html gets replaced correctly but then I have the above error. It is as if, after rendering the create_image method it has to keep going and render something else, so the render to parent calls the create_image.rjs correctly but keeps going like a runaway train.
Any ideas? Is there something I can put at the bottom of my create_image.rjs to keep it from continuing after it renders itself correctly?
thanks!
code below:
Here is the whole controller create method, highlight_images_controller.create_image:
def create_image @facebook_id = params[:facebook_id] if params[:highlight_image][:uploaded_data].blank? success = true @blank_data = true #@highlight = Highlight.find(params[:highlight_id]) else @h = Highlight.find(params[:highlight_id]) @h.cleanup_images!(@h.id) success = false begin @highlight_image = HighlightImage.create(params[:highlight_image]) @highlight = Highlight.find(@highlight_image.highlight_id) if @highlight_image.save
#success = true end success = true rescue => ex flash[:notice] = "There was a problem uploading your HighlightImage photo. Please try again." logger.error "Error occured while uploading a HighlightImage for highlight #{params[:highlight_image][:highlight_id]}: #{ex.message}" logger.error ex.backtrace.join("\n"); end end
respond_to do |format| format.js do if success responds_to_parent { render :action => "create_image.rjs" } end end
end end
Here is the view calling it: _upload_image_form.rhtml:
<div id='upload_image'> <%= error_messages_for :highlight_image %>
<% form_for :highlight_image, :url => url_for(:highlight_id => @highlight.id, :action => 'create_image'), :html => { :multipart => true, :target => 'upload_frame' } do |form| %> <%= form.file_field :uploaded_data %> <%= form.hidden_field :highlight_id, :value => @highlight.id %>
<br/><br/> <div id="avatar_action"> <%= submit_tag "Upload Image", :onClick=>"$('avatar_action').hide();$ ('avatar_progress').show();" %> or <a href="#" onClick="javascript:closeCurrentOverlay();">cancel</a> </div> <div id="avatar_progress" style="display:none;"> <%= image_tag('progress_bar.gif') %> </div> <br/><br/> <% end -%> <iframe id='upload_frame' name="upload_frame" style="width:1px;height: 1px;border:0px" src="about:blank"></iframe> </div>
And here is the rjs call:
create-image.rjs:
page.call 'closeCurrentOverlay' unless @blank_data page.replace_html "display_highlight_image", display_highlight_image(@highlight) end
Here is the routing: (from routes.rb) map.resources :highlights do |highlight| highlight.resources :highlight_images, :controller=>'highlights/ highlight_images' end