Forum: Ruby on Rails RHTML and innerHTML problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F1b0c5e0a1032e096a2888b61abc53c3?d=identicon&s=25 Tom V. (Guest)
on 2007-05-09 13:54
(Received via mailing list)
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.
8d1e089fd6a6703f5f13dbee271aa77c?d=identicon&s=25 Yuri Leikind (Guest)
on 2007-05-09 14:16
(Received via mailing list)
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. <ruby-talk@hinv.org> wrote:
>   var rhtmlCode = "<"+"%= live_tree :fstree, ...%>";
> Tom.
>
> >
>


--
Best regards,
Yuri Leikind
3a0f2ec009ceffdd65f056540a409a23?d=identicon&s=25 Chris Mear (Guest)
on 2007-05-09 14:19
(Received via mailing list)
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
F1b0c5e0a1032e096a2888b61abc53c3?d=identicon&s=25 Tom V. (Guest)
on 2007-05-09 14:38
(Received via mailing list)
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.
F1b0c5e0a1032e096a2888b61abc53c3?d=identicon&s=25 Tom V. (Guest)
on 2007-05-09 14:56
(Received via mailing list)
Hi Yuri,

thanks for the clarification!
And you do understand me right ;-)

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.
1e8f141e7857d397d8020ed3b759e88a?d=identicon&s=25 Maciej Piechotka (Guest)
on 2007-05-09 15:21
(Received via mailing list)
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/)
This topic is locked and can not be replied to.