# Link\_to\_remote udpates a div w/ javascript (Greybox) help

**URL:** <https://www.ruby-forum.com/t/link-to-remote-udpates-a-div-w-javascript-greybox-help/140022>\
**Category:** Rails\
**Created:** [June 10, 2008, 5:26am UTC](https://www.ruby-forum.com/t/link-to-remote-udpates-a-div-w-javascript-greybox-help/140022 "2008-06-10T05:26:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![freaknasty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@freaknasty](https://www.ruby-forum.com/u/freaknasty)\
**Post date:** [June 10, 2008, 5:26am UTC](https://www.ruby-forum.com/t/link-to-remote-udpates-a-div-w-javascript-greybox-help/140022/1 "2008-06-10T05:26:45Z")

</div>

Hi all,

Once again I’m looking for all y’alls help. Currently I have a  
link\_to\_remote as follows:

\<%=link\_to\_remote(product.name, :update =\> “selected\_product”, :url =\>  
{:action =\> “show”, :id =\> product.code})%\>

This link updates a div called selected\_product. On the page that is  
rendered in the updated div, I use a Greybox plugin to show images, but  
when the div is updated, the Greybox is disabled for some reason. I  
found this site which seemed to fix my problem temporarily.

[http://www.intridea.com/2007/11/21/link\_to\_remote-unobtrusively](http://www.intridea.com/2007/11/21/link_to_remote-unobtrusively)

It worked for some reason without having to re-do the link\_to\_remote  
helper, but after restarting my Webrick I get this error now:

try { } catch (e) { alert(‘RJS error:\n\n’ + e.toString()); alert(‘’);  
throw e }

Here is my controller code:

def show  
@product = Product.find\_by\_code(params[:id])  
if !@product  
flash[:notice] = “Sorry, we couldn’t find the product you were  
looking for”  
redirect\_to :action =\> ‘index’ and return false  
end  
@title = @product.name  
@images = @product.images.find(:all)  
@default\_image = @images[0]  
@variations = @product.variations.find(  
:all,  
:order =\> ‘name ASC’,  
:conditions =\> ‘quantity \> 0’  
)  
respond\_to do |format|  
format.html{redirect\_to :back}  
format.js{render :update do |page|

```
end }

```

end  
end

The respond\_to is a portion from the site above.

I tried the above link suggestion to edit the helper, but I get a error  
stating

super: no superclass method `link\_to\_remote’

So my question is using a link\_to\_remote, how can javascript be enabled  
in an updated div?

---

<div class="post-metadata">

**Author:** ![freaknasty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@freaknasty](https://www.ruby-forum.com/u/freaknasty)\
**Post date:** [June 10, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/link-to-remote-udpates-a-div-w-javascript-greybox-help/140022/2 "2008-06-10T16:28:37Z")

</div>

The page that you’re showing alludes to the fact that it is an  
incomplete example. The comment that they’ve included in the  
render :update block (update the page dynamically) is not a statement  
indicating what happens as a result of executing the block – it’s a  
direction to you to do the work.

In the ‘big picture’, you’re using two overlapping methods to update  
the div. Using the :update parameter on link\_to\_remote causes the  
framework to generate an Ajax.Updater call that expects an html  
response; the Updater will take the contents of the response and paste  
them into the dom element you specify. If you use this approach  
then your format.js actually needs to render the _html_ that the  
Ajax.Updater needs to update the ‘selected\_product’ div.

The ‘render :update’ call on the server attempts to do something very  
similar. It assumes that you’ve made a request for javascript and  
provides a number of convenience methods for writing that javascript  
response. (It’s the same thing as RJS, if you’re read about that).  
If you use this approach you need to have your action use something  
like

page.replace\_html ‘selected\_product’, :partial=\>‘some-partial-that-  
renders-the-product-info’

Unfortunately, the two methods cannot be used together. (If you think  
about it, this makes sense… you’re sending a request that expects  
html and sending a response in javascript). The javascript error that  
you’re seeing – the empty js try-catch – is the default  
implementation of render(:update) with nothing added. If you’re only  
updating one element on the page (as it sounds you are) then you’re  
better off keeping the :update parameter and changing your format.js  
to render the html that it expects. If you need to update the page in  
more than one place then drop the :update parameter and write all the  
update in the render(:update) block. In the latter case you are  
actually better off writing an rjs file to promote better separation  
in your code (e.g., show.js.rjs). Rjs files need only contain the  
‘guts’ of the js response; they are automatically wrapped in the  
render(:update) block for you.

HTH,  
AndyV

---

<div class="post-metadata">

**Author:** ![freaknasty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@freaknasty](https://www.ruby-forum.com/u/freaknasty)\
**Post date:** [June 11, 2008, 5:10am UTC](https://www.ruby-forum.com/t/link-to-remote-udpates-a-div-w-javascript-greybox-help/140022/3 "2008-06-11T05:10:34Z")

</div>

Andy, I truly appreciate your quick reply and help but well I’m still a  
noob so comprehending everything might be a little too much. I do want  
to stick with the :update parameter but don’t quite understand what you  
meant by changing the “format.js to render the html”. Could you please  
elaborate.

AndyV wrote:  
If you’re only

> updating one element on the page (as it sounds you are) then you’re  
> better off keeping the :update parameter and changing your format.js  
> to render the html that it expects. If you need to update the page in  
> more than one place then drop the :update parameter and write all the  
> update in the render(:update) block. In the latter case you are  
> actually better off writing an rjs file to promote better separation  
> in your code (e.g., show.js.rjs). Rjs files need only contain the  
> ‘guts’ of the js response; they are automatically wrapped in the  
> render(:update) block for you.
> 
> HTH,  
> AndyV
