Forum: Ruby on Rails Calling JS from controller and RJS

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9e10e137ffc479b9fc540c19d73f94ff?d=identicon&s=25 MaM o_0 (mravo)
on 2007-01-22 10:31
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!
5f23b06d22b84ec6c902bf390071a79c?d=identicon&s=25 _Kevin (Guest)
on 2007-01-22 13:37
(Received via mailing list)
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
9e10e137ffc479b9fc540c19d73f94ff?d=identicon&s=25 MaM o_0 (mravo)
on 2007-01-22 15:21
_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!
9e10e137ffc479b9fc540c19d73f94ff?d=identicon&s=25 MaM o_0 (mravo)
on 2007-01-22 15:25
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!
9e10e137ffc479b9fc540c19d73f94ff?d=identicon&s=25 MaM o_0 (mravo)
on 2007-01-22 15:31
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!
This topic is locked and can not be replied to.