render / responds to parent / rjs question

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

gmg_dev wrote:

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.

Hmm... I read through your post, and understand the points you make above, but after I saw the controller code I thought I heard the muffled cry of a dying kitten.

DHH once said (and I'm paraphrasing), sometimes when you think you're in a hurry, the best thing to do is slow down and do it right.

That said, I think you should look at AttachmentFu. It's _very_ easy to use and the documentation is great. It'll simplify your controller code immensely, which in turn will give you some breathing room to concentrate on your view and RJS.

Even if you don't want to (or can't) use AttachmentFu, I imagine it would be a net positive if instead of hacking code you may not fully understand, you "slow down", undo the damage and correct things now.

I apologize in advance if I sound pretentious or condescending -- it's not my goal. I've been in your situation before and I can safely say the times I simply bored ahead, hacking and slashing my way to a solution were far less satisfying (and sturdy!) than the times I rewrote what I knew was bad code.