Calling JS from controller and RJS


#1

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!


#2

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/.

How about this… in your controllerj

def some_action
render :update do |page|
page.call “JSfoo”
end
end

_Kevin


#3

_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!


#4

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!


#5

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!