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:
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"
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.