# Render RJS in Index

**URL:** <https://www.ruby-forum.com/t/render-rjs-in-index/88462>\
**Category:** Rails\
**Created:** [March 11, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/render-rjs-in-index/88462 "2007-03-11T15:25:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 11, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/render-rjs-in-index/88462/1 "2007-03-11T15:25:51Z")

</div>

Greetings

Having the darndest time trying to render an inline RJS page element  
in my index page. I don’t have any trouble linking to a remote page  
and rendering RJS, but it’s got me stumped how to do it from the index  
page. I guess I have a basic gap in my understanding of how RJS works.

A simple example would be to render page.alert(‘hello world!’) when  
the user opens a support index page:

[http://example.com/support](http://example.com/support)

where the support controller action index is:

def index  
render :update do |page|  
page.alert(‘hello world!’)  
end  
end

Which dumps raw Javascript to the web page:

try {  
alert(“hello world!”);  
} catch (e) { alert(‘RJS error:\n\n’ + e.toString());  
alert(‘alert("hello world!");’); throw e }

Thanks for the help!

Dave

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 11, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/render-rjs-in-index/88462/2 "2007-03-11T15:44:52Z")

</div>

Hi –

On 3/11/07, Dave [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Which dumps raw Javascript to the web page:
> 
> try {  
> alert(“hello world!”);  
> } catch (e) { alert(‘RJS error:\n\n’ + e.toString());  
> alert(‘alert("hello world!");’); throw e }

The usual cause of this is that you have an :update parameter in your  
view, so it updates the element with the raw javascript.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 11, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/render-rjs-in-index/88462/3 "2007-03-11T16:05:54Z")

</div>

> A simple example would be to render page.alert(‘hello world!’) when  
> the user opens a support index page:

The way RJS works (notice is an abstraction, so no details given) is  
something like this. You already have a document loaded in your browser,  
and from this document you make a remote request, which will basically  
invoke a browser-side object capable of calling the remote point and,  
after getting a response, it will execute the remote response against  
your javascript document object.

This means the entry point for a RJS to work is having a document in  
which to apply the changes. That document is what rails gives you in the  
“page” object.

If you directly call a RJS action, what you get is the javascript that  
would execute against your document object. Since you don’t have any and  
you are not even in javascript context, you get just the plain text in  
your browser. That’s the reason many RJS actions have something like  
“return unless request.xhr?” meaning something like “do nothing unless  
we are being called via ajax”

Regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 11, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/render-rjs-in-index/88462/4 "2007-03-11T16:31:57Z")

</div>

Very good. Now I understand why the Javascript is not getting  
rendered. I suppose wrapping script tags around the output will force  
it to render, but then I should be using erb, which is a little ugly,  
but works.

What I’m after is calling a Javascript object within the view from the  
index page, so that no further clicks are needed. Using erb should do  
it.

Thanks for the help!  
Dave

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 11, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/render-rjs-in-index/88462/5 "2007-03-11T16:12:55Z")

</div>

Hi –

On 3/11/07, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The usual cause of this is that you have an :update parameter in your  
> view, so it updates the element with the raw javascript.

Well, technically my statement is correct – that is the usual cause  
– but in this case, see Javier’s answer rather than mine 🙂

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
