# Render :update and script tags

**URL:** <https://www.ruby-forum.com/t/render-update-and-script-tags/78385>\
**Category:** Rails\
**Created:** [December 5, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/render-update-and-script-tags/78385 "2006-12-05T13:43:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [December 5, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/render-update-and-script-tags/78385/1 "2006-12-05T13:43:23Z")

</div>

Hi All!

I’m kind of new o ruby so please apologize my stupid questions on  
advanced…

I’m using  
render :update do |page|  
page.replaca\_html :element , :partial =\> ‘apartial’  
end

This is working well, since I see in the page the following code:

try  
{  
Element.update(“element”, “”);  
} catch (e)  
{  
alert(‘RJS error:\n\n’ + e.toString());  
alert(‘Element.update(“element”, “”);’);  
throw e  
}

I simplified the output.

My problem is that no script tag is generated and the code is not  
executed!

How can I fix this?

---

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [December 5, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/render-update-and-script-tags/78385/2 "2006-12-05T13:51:03Z")

</div>

Pedro C. wrote:

> I’m using  
> render :update do |page|  
> page.replaca\_html :element , :partial =\> ‘apartial’  
> end

Is this inside a view, or up in a controller?

> }
> 
> I simplified the output.
> 
> My problem is that no script tag is generated and the code is not  
> executed!
> 
> How can I fix this?

Inside a view, use update\_page\_tag(). I think render :update works  
different  
inside a view. You could, for example, stack up many render :updates  
inside  
one view tag.

But prefer using a controller, because “that’s what everyone else does”.  
If  
possible!

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!n

---

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [December 5, 2006, 2:01pm UTC](https://www.ruby-forum.com/t/render-update-and-script-tags/78385/3 "2006-12-05T14:01:05Z")

</div>

On 5 December 2006 15:43, Pedro C. wrote:

> I’m using  
> render :update do |page|  
> page.replaca\_html :element , :partial =\> ‘apartial’  
> end
> 
> My problem is that no script tag is generated and the code is not  
> executed!

What did you expect ? You’ve asked controller to update HTML, so  
controller  
generated javascript to do that. You can then execute it on page.  
But if you used e.g. remote\_function with :update parameter, then  
Ajax.Request  
expect some HTML from server to put into element you’ve specified.

I think it is (almost always) better to use remote\_function without  
:update  
argument, because it gives you more flexibility (not just updating  
contents  
of single element).

---

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [December 5, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/render-update-and-script-tags/78385/4 "2006-12-05T14:15:08Z")

</div>

render :update is meant be used in the controller as a response to an  
AJAX request, it returns pure javascript that will be executed by tha  
AJAX code that called the action … so of course there are no   
tags.

---

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [December 5, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/render-update-and-script-tags/78385/5 "2006-12-05T14:36:05Z")

</div>

I used it in the controller.

And it did what I was expecting!

Anyway, I’m using this do display the errors in a specific part of my  
html, so this is no response to a request.

The following quote is from the manual “Use this in your Ajax response  
bodies, either in a tag or as plain JavaScript sent with a  
Content-type of “text/javascript””.

So, this is my question, how do I do that? How can I add the script  
tags?

---

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [December 5, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/render-update-and-script-tags/78385/6 "2006-12-05T22:06:42Z")

</div>

Pedro C. wrote:

> Anyway, I’m using this do display the errors in a specific part of my  
> html, so this is no response to a request.

Everything’s a responds to a request. Someone asked if you were doing  
a response to an Ajax request. You are apparently injecting JavaScript  
when the page first paints.

> The following quote is from the manual “Use this in your Ajax response  
> bodies, either in a ’ around the string it returns.
> 
> If you are inside a view, go
> 
> Note that there are few reasons to do the former. Yet there might be  
> many reasons to do the latter - sharing the first-paint code with the  
> update code, for example.
> 
> And note a purist would need the type=‘text/javascript’, the // CDATA  
> escape things. I don’t know what function returns those.
> 
> –  
> Phlip  
> [http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!
