# Calling JS from controller and RJS

**URL:** <https://www.ruby-forum.com/t/calling-js-from-controller-and-rjs/82908>\
**Category:** Rails\
**Created:** [January 22, 2007, 10:31am UTC](https://www.ruby-forum.com/t/calling-js-from-controller-and-rjs/82908 "2007-01-22T10:31:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mravo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@mravo](https://www.ruby-forum.com/u/mravo)\
**Post date:** [January 22, 2007, 10:31am UTC](https://www.ruby-forum.com/t/calling-js-from-controller-and-rjs/82908/1 "2007-01-22T10:31:28Z")

</div>

Hi,  
I have solved this issue by myself, but I think it’s a dirty way to go.  
Please if someone knows a better way to do it, please let me know.

This is how I call JS from the controller  
controller.rb:  
def someAction  
render :text =\> “JSfoo()”,  
:content\_type =\> “text/javascript”  
end

and this is how I call it from RJS  
RJSfoo.rjs:  
page \<\< ‘JSfoo()’

Why doesn’t the RJS method work in the controller (I tried it), and is  
there a better way to call JS from the controller?  
Thanks!

---

<div class="post-metadata">

**Author:** ![mravo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@mravo](https://www.ruby-forum.com/u/mravo)\
**Post date:** [January 22, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/calling-js-from-controller-and-rjs/82908/2 "2007-01-22T13:37:42Z")

</div>

MaM o\_0 wrote:

> and this is how I call it from RJS  
> RJSfoo.rjs:  
> page \<\< ‘JSfoo()’
> 
> Why doesn’t the RJS method work in the controller (I tried it), and is  
> there a better way to call JS from the controller?  
> Thanks!
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

How about this… in your controllerj

def some\_action  
render :update do |page|  
page.call “JSfoo”  
end  
end

\_Kevin

---

<div class="post-metadata">

**Author:** ![mravo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@mravo](https://www.ruby-forum.com/u/mravo)\
**Post date:** [January 22, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/calling-js-from-controller-and-rjs/82908/3 "2007-01-22T15:21:13Z")

</div>

\_Kevin wrote:

> How about this… in your controllerj
> 
> def some\_action  
> render :update do |page|  
> page.call “JSfoo”  
> end  
> end
> 
> \_Kevin

Thank you \_Kevin! Works like a charm ;). If you would tell me how to  
pass an argument to the JS function, you’d be a life-saver!  
Thanks!

---

<div class="post-metadata">

**Author:** ![mravo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@mravo](https://www.ruby-forum.com/u/mravo)\
**Post date:** [January 22, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/calling-js-from-controller-and-rjs/82908/4 "2007-01-22T15:25:32Z")

</div>

I figured that one on my own, here’s my way of doing it:

def some\_action  
@some\_arg = 3  
render :update do |page|  
page.call (“JSfoo”, @some\_arg)  
end  
end

If you know a better way, please let me know, or how could this argument  
be an (javascript) array. Maybe that’s too much;)?  
cheers!

---

<div class="post-metadata">

**Author:** ![mravo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@mravo](https://www.ruby-forum.com/u/mravo)\
**Post date:** [January 22, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/calling-js-from-controller-and-rjs/82908/5 "2007-01-22T15:31:25Z")

</div>

I’m again answering my own question, I hope it’ll help someone else.  
Here is how I pass an array to javascript function.

def some\_action  
@some\_arg = [1,2,3]  
render :update do |page|  
page.call (“JSfoo”, @some\_arg)  
end  
end

Ruby on Rails is really great!
