Topic: invoking JavaSCript/RJS from controller

I need to invoke a javascript function from my controller. Does anyone know how to do this?

To give a bigger picture. The user inputs a postcode. If I can find this postcode in my coordinates database, I use these coordinates. If I can't, I need to call a JS (Google AJAX Search API method) to geocode the postcode, and use the returned coordinates.

I have tried to create a RJS template which calls the geocoding function. I tried doing this by redirect_to :action => 'method'. This showed the RJS file in plain text!

I'm quite lost in this. Any help is greatly appreciated.

Re: invoking JavaSCript/RJS from controller

Try the call method in your rjs template:

page.call :foo

Or you can input javascript directly with <<

page << "foo()"

Railscasts - Free Ruby on Rails Screencasts

Re: invoking JavaSCript/RJS from controller

Thanks for the quick reply RyanB.

That's not quite what I'm looking to do.

Currently, my rjs template is being displayed as plain text, as in another controller action, I am redirect_to ing to it. I am doing this because I want to initiate the ajax call from my controller.

Thinking about this since my previous post, I don't think calling AJAX from a Rails controller would work.

What I'll have to do is:
1) load a page with ajax call to geocode method
2) call geocode method on page load. auto submit data to my controller via ajax
3) display results via ajax

Re: invoking JavaSCript/RJS from controller

Hello!

Following article contains solution. See section Inline Rendering

http://ajax.stealthsettings.com/rjs-tem … and-rails/

Last edited by alno (2007-08-19 15:47:03)