# Link\_to\_remote gives a javascript error

**URL:** <https://www.ruby-forum.com/t/link-to-remote-gives-a-javascript-error/171307>\
**Category:** Rails\
**Created:** [June 30, 2009, 10:40am UTC](https://www.ruby-forum.com/t/link-to-remote-gives-a-javascript-error/171307 "2009-06-30T10:40:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colorblind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@Colorblind](https://www.ruby-forum.com/u/Colorblind)\
**Post date:** [June 30, 2009, 10:40am UTC](https://www.ruby-forum.com/t/link-to-remote-gives-a-javascript-error/171307/1 "2009-06-30T10:40:53Z")

</div>

I’m using this code in controller when updating the page:

```
render :update do |page|
    ...
    for p in @cat_photos
      page << "$('#flyingCats').html($(this).html() + '" +

```

link\_to\_remote(image\_tag(p.image.url(:thumb)),  
{:url =\> {:controller =\> “photos” ,:action  
=\> :refresh\_image,  
:id =\> p.id}, :method =\> :get}, { :href =\> “#” })

- “’);”  
end  
…  
end

And, though, I’m using exactly this code in the other part of app and  
it works, i get a strange js error in FireBug:

“missing ) after argument list  
[Break on this error] $(’#flyingCats’).html($(this).html() +  
‘…h\_image?id=11’}); return false;”\>\<img al…"

---

<div class="post-metadata">

**Author:** ![Colorblind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@Colorblind](https://www.ruby-forum.com/u/Colorblind)\
**Post date:** [June 30, 2009, 1:12pm UTC](https://www.ruby-forum.com/t/link-to-remote-gives-a-javascript-error/171307/2 "2009-06-30T13:12:56Z")

</div>

On Jun 30, 4:40 am, Colorblind [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m using this code in controller when updating the page:
> 
> ```
> render :update do |page|
> ...
> for p in @cat_photos
> page << "$('#flyingCats').html($(this).html() + '" +
> 
> ```
> 
> link\_to\_remote(image\_tag(p.image.url(:thumb)),

Have you looked at what this expands to ? I wouldn’t be surprised if  
what the link\_to\_remote expands to included a quote mark which would  
screw your stuff up.

Fred

---

<div class="post-metadata">

**Author:** ![Colorblind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@Colorblind](https://www.ruby-forum.com/u/Colorblind)\
**Post date:** [June 30, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/link-to-remote-gives-a-javascript-error/171307/3 "2009-06-30T13:18:13Z")

</div>

Bug fixed by putting link\_to\_remote in escape\_javascript function:

```
      page << "$('#flyingCats').html($(this).html() + '" +

```

escape\_javascript(link\_to\_remote(image\_tag(p.image.url(:thumb)), {:url  
=\> {:controller =\> ‘photos’, :action =\> :refresh\_image, :id =\>  
p.id}, :method =\> :get})) + “’);”

I hope my solution would be useful.

---

<div class="post-metadata">

**Author:** ![Colorblind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@Colorblind](https://www.ruby-forum.com/u/Colorblind)\
**Post date:** [July 1, 2009, 6:21pm UTC](https://www.ruby-forum.com/t/link-to-remote-gives-a-javascript-error/171307/4 "2009-07-01T18:21:07Z")

</div>

Colorblind wrote:

> Bug fixed by putting link\_to\_remote in escape\_javascript function:  
> […]

Glad to hear it!

Now extract the JS code from your view into a separate file so that you don't have to use escape\_javascript.

(JS doesn’t belong in HTML.)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
