# Quick RJS question

**URL:** <https://www.ruby-forum.com/t/quick-rjs-question/85298>\
**Category:** Rails\
**Created:** [February 12, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/quick-rjs-question/85298 "2007-02-12T13:16:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathias\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@Mathias\_W](https://www.ruby-forum.com/u/Mathias_W)\
**Post date:** [February 12, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/quick-rjs-question/85298/1 "2007-02-12T13:16:54Z")

</div>

I got a short question about RJS.

Is it possible to get the RJS to “compile” to Javascript code without  
having to use AJAX? As in, can I use RJS to bundle some javascript  
code with my page when it’s first created?

Thanks,  
Mathias W…

---

<div class="post-metadata">

**Author:** ![Mathias\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@Mathias\_W](https://www.ruby-forum.com/u/Mathias_W)\
**Post date:** [February 12, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/quick-rjs-question/85298/2 "2007-02-12T13:27:56Z")

</div>

You can use all the helpers you use in a RJS template in a vie as  
well, you just dont need the page obejct.  
you can also use the JavaScript Helpers to create script tags, links  
with function calls etc.for this.

link\_to\_function “hide menu”, visual\_effect(:fade, “menu”)

On 12 Feb., 13:15, “Mathias W.” \<wittlock

---

<div class="post-metadata">

**Author:** ![Mathias\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@Mathias\_W](https://www.ruby-forum.com/u/Mathias_W)\
**Post date:** [February 12, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/quick-rjs-question/85298/3 "2007-02-12T14:55:07Z")

</div>

On 2/12/07, Thorsten L [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can use all the helpers you use in a RJS template in a vie as  
> well, you just dont need the page obejct.  
> you can also use the JavaScript Helpers to create script tags, links  
> with function calls etc.for this.
> 
> link\_to\_function “hide menu”, visual\_effect(:fade, “menu”)

Hmm, this pointed me in the right direction to get what I wanted.  
Thanks a bunch, much appreciated! 🙂

Mathias.

---

<div class="post-metadata">

**Author:** ![Mathias\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@Mathias\_W](https://www.ruby-forum.com/u/Mathias_W)\
**Post date:** [February 12, 2007, 3:41pm UTC](https://www.ruby-forum.com/t/quick-rjs-question/85298/4 "2007-02-12T15:41:15Z")

</div>

On 2/12/07, Mathias W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I got a short question about RJS.
> 
> Is it possible to get the RJS to “compile” to Javascript code without  
> having to use AJAX? As in, can I use RJS to bundle some javascript  
> code with my page when it’s first created?

Thorsten gave you one answer—probably the one you wanted. Another  
answer is simply “yes”. You can use te JavaScriptGenerator which RJS  
invokes in any view context (or instantiate it manually if you want to  
go through those pains). Here’s what it looks like:

update\_page do |page|  
page[‘an-element’].visual\_effect :shake  
end

This block will actually return the generated Javascript. So, for  
example, if you wanted to do your own special link\_to\_shake helper,  
you could do something like this (untested):

def link\_to\_shake(element\_id)  
link\_to\_function “Shake it”, update\_page do |page|  
page[element\_id].visual\_effect :shake  
end  
end

Chad

---

<div class="post-metadata">

**Author:** ![Mathias\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@Mathias\_W](https://www.ruby-forum.com/u/Mathias_W)\
**Post date:** [February 12, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/quick-rjs-question/85298/5 "2007-02-12T16:04:19Z")

</div>

This could be usefull in debugging RJS output for those of us that  
have no clue about RJS and want to see the javascript it’s  
generating…

If I were writting normal javascript I would do:

$(‘attribute[\<%=index%\>][id\_to\_me]’).value = “”;  
$(‘filename- \<%=index%\>’).value = “”;

but I want to return javascript as an Ajax response so I’m doing:

page.select("#attribute[#{index}][id\_to\_me]").value = “”;  
page.select("#filename-#{index}").value = “”;

And it’s not working, and I’m not really sure how to debug it.

---

<div class="post-metadata">

**Author:** ![Mathias\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@Mathias\_W](https://www.ruby-forum.com/u/Mathias_W)\
**Post date:** [February 12, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/quick-rjs-question/85298/6 "2007-02-12T16:08:20Z")

</div>

> page.select("#attribute[#{index}][id\_to\_me]").value = “”;  
> page.select("#filename-#{index}").value = “”;

you don’t use the # at the start of the id’s name in RJS, simply  
because it’s not used in Prototype Javascript as well.

page.select(“attribute[#{index}][id\_to\_me]”).value = “”;  
page.select(“filename-#{index}”).value = “”;

---

<div class="post-metadata">

**Author:** ![Mathias\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@Mathias\_W](https://www.ruby-forum.com/u/Mathias_W)\
**Post date:** [February 12, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/quick-rjs-question/85298/7 "2007-02-12T16:10:20Z")

</div>

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

> but I want to return javascript as an Ajax response so I’m doing:
> 
> page.select(“#attribute[#{index}][id\_to\_me]”).value = “”;  
> page.select(“#filename-#{index}”).value = “”;
> 
> And it’s not working, and I’m not really sure how to debug it.

The best way is to use FireFox with FireBug. You can inspect the  
response of an XMLHTTPRequest and see the generated Javascript. Very  
handy.

Chad
