RHTML and innerHTML problem

Hi,

I am trying to render RHTML code
using a JavaScript function and innerHTML.

However, the code is shown as plain text
but is not rendered.

function makeLiveTree() {
var rhtmlCode = “<”+"%= live_tree :fstree, …%>";
document.getElementById(‘westContent’).innerHTML = rhtmlCode;
}

output is:
<%= live_tree :fstree, …%>

How do I render RHTML using JS?
For this problem I cannot use RJS.

Thanks for your time,
Tom.

Tom,

If I understand you correctly, you are trying to create some dynamic
tree in the browser page. The way you want to do it won’t work because
whatever your JS code generates, this is the client side, the page
loaded into the user’s browser, and there’s no Rails there to evaluate
it.

Use Rails Ajax capabilities, it’s really simple. Read about methods
ActionView::Helpers::PrototypeHelper, methods which have the word
‘remote’ in their names. Instead of JS generating new content for your
page there will be partial templates which will be evaluated and the
output will be inserted into your page automagically without reloading
it.

Cheers,
Yuri

On 5/9/07, Tom V. [email protected] wrote:

var rhtmlCode = “<”+“%= live_tree :fstree, …%>”;
Tom.


Best regards,
Yuri L.

You can’t render RHTML using JS. Your RHTML files are processed on the
server, and then the resulting HTML is sent to the browser. Your
JavaScript code runs in the browser, so if you generate RHTML in the
browser, it will not be processed.

If you need to request new live content from the server, then you will
have to use some kind of AJAX, whether or not you specifically use RJS
to do it.

Or, if the output of “live_tree :fstree” can be generated at the same
time as the overall page, then just render it as normal, into a hidden
div or a JavaScript variable, or something like that. Then the output
of “live_tree :fstree” will be in the browser when the page loads, and
you can then use JavaScript to display it as necessary.

Chris

Hi Chris,

thanks for clarifying that!

The output of “live_tree :fstree” actually IS rendered
when the overall page is loaded.
My problem is that I need to reload the fstree
everytime a user creates a new file.
The code I posted is just to test “reloading”…

I tried to reload live_tree using “render :partial”.
Its output is rendered,
however the whole layout of my site gets broken,
all CSS is gone, Firebugs throws millions of errors…
If I set “:layout => false” nothing is rendered
If I set “:layout => ‘myDefaultLayout’”, everything breaks apart.

Any idea?
Thanks for your help!
Tom.

Hi Yuri,

thanks for the clarification!
And you do understand me right :wink:

This rhtml/innerhtml thing actually was only
to test “reloading” the tree.

I am able to generate and render the tree
but only when the whole page is loaded.
What I wanna do is reload just the tree, not the whole page.

I tried to use “render :partial”
but that did not work.

using “:layout => ‘myDefaultLayout’” obviously cannot work.
setting layout to false breaks the whole layout.
This is because “live_tree” tries to expand as much as possible.
the author recommends to use a surrounding div with height and width
specified, however that does not work for me.

Thanks for your time.
Tom.

On Wed, 09 May 2007 13:54:44 +0200, Tom V. wrote:

}

output is:
<%= live_tree :fstree, …%>

How do I render RHTML using JS?
For this problem I cannot use RJS.

Thanks for your time,
Tom.

It’s impossible - JS is client side technology and rhtml is server side.
Ruby server can generate JavaScript code and send it to client but
client
cannot send code to server to execute (first of all it would be
dangerous). You can try do it inside rhtml page:
function makeLiveTree() {
document.getElementById(‘westContent’).innerHTML = <%= live_tree
:fstree %>
}

Regards

I’ve probably left my head… somewhere. Please wait untill I find it.
Homepage (pl_PL): http://uzytkownik.jogger.pl/
(GNU/)Linux User: #425935 (see http://counter.li.org/)