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 –

On 3/11/07, Dave [email protected] wrote:

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 }

The usual cause of this is that you have an :update parameter in your
view, so it updates the element with the raw javascript.

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

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

Estamos de estreno… si necesitas llevar el control de tus gastos
visita http://www.gastosgem.com !!Es gratis!!

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

Hi –

On 3/11/07, David A. Black [email protected] wrote:

The usual cause of this is that you have an :update parameter in your
view, so it updates the element with the raw javascript.

Well, technically my statement is correct – that is the usual cause
– but in this case, see Javier’s answer rather than mine :slight_smile:

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)