Render RJS in Index

Greetings

Having the darndest time trying to render an inline RJS page element in my index page. I don't have any trouble linking to a remote page and rendering RJS, but it's got me stumped how to do it from the index page. I guess I have a basic gap in my understanding of how RJS works.

A simple example would be to render page.alert('hello world!') when the user opens a support index page:

http://example.com/support

where the support controller action index is:

def index   render :update do |page|     page.alert('hello world!')   end end

Which dumps raw Javascript to the web page:

try { alert("hello world!"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('alert(\"hello world!\");'); throw e }

Thanks for the help!

Dave

Hi --

A simple example would be to render page.alert('hello world!') when the user opens a support index page:   

The way RJS works (notice is an abstraction, so no details given) is something like this. You already have a document loaded in your browser, and from this document you make a remote request, which will basically invoke a browser-side object capable of calling the remote point and, after getting a response, it will execute the remote response against your javascript document object.

This means the entry point for a RJS to work is having a document in which to apply the changes. That document is what rails gives you in the "page" object.

If you directly call a RJS action, what you get is the javascript that would execute against your document object. Since you don't have any and you are not even in javascript context, you get just the plain text in your browser. That's the reason many RJS actions have something like "return unless request.xhr?" meaning something like "do nothing unless we are being called via ajax"

Regards,

javier ramirez

Hi --

Very good. Now I understand why the Javascript is not getting rendered. I suppose wrapping script tags around the output will force it to render, but then I should be using erb, which is a little ugly, but works.

What I'm after is calling a Javascript object within the view from the index page, so that no further clicks are needed. Using erb should do it.

Thanks for the help! Dave