# Render / responds to parent / rjs question

**URL:** <https://www.ruby-forum.com/t/render-responds-to-parent-rjs-question/135158>\
**Category:** Rails\
**Created:** [April 18, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/render-responds-to-parent-rjs-question/135158 "2008-04-18T16:44:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmg\_dev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@gmg\_dev](https://www.ruby-forum.com/u/gmg_dev)\
**Post date:** [April 18, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/render-responds-to-parent-rjs-question/135158/1 "2008-04-18T16:44:12Z")

</div>

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](http://localhost:3000/highlights/73/highlight_images/new)  
(754ms)prototype.js (line 1223)  
loc is not defined  
[Break on this error] Action Controller: Exception caught\</  
title\>  
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:

\<%= 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 %\>

 \<%= submit\_tag "Upload Image", :onClick=\>"$('avatar\_action').hide();$ ('avatar\_progress').show();" %\> or cancel

 \<%= image\_tag('progress\_bar.gif') %\> 
  
  
\<% end -%\>

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

---

<div class="post-metadata">

**Author:** ![gmg\_dev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@gmg\_dev](https://www.ruby-forum.com/u/gmg_dev)\
**Post date:** [April 18, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/render-responds-to-parent-rjs-question/135158/2 "2008-04-18T20:09:20Z")

</div>

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.
