Skip to content Skip to sidebar Skip to footer

Rendering A Html.erb In Rails With Javascript

I create a page and some parts of it are layout files that are rendered. However I want to render one of these rendered part again with javascript code. I want to render these part

Solution 1:

You shouldn't attempt to render erb with javascript - it's not how it's done (not to mention you'd have to write yourself a erb parser and eventually call the server for data anyway).

If on the other hand, you're asking how to issue the render of some template using Javasript and then replace old content with new one then...

Watch this http://railscasts.com/episodes/205-unobtrusive-javascript, read this Rails 3 and RJS to get the idea of how to trigger some content updates via javascript.

General idea is

  1. Render the initial full page
  2. Trigger ajax call (either after some time or by manual user request) to do a request to your rails application
  3. Handle that request and respond to it with javascript script (the script content should contain all the functionality and content to replace what you want)

Solution 2:

It's possible to share templates between the server and the client. One library that allows for this is called mustache.

But if this is the only case you need it I'd go with the RJS approach as well.

Solution 3:

Instead of rendering I did it AJAX way and update it on the page. Thanks for the answers but I did not use any of these.

Post a Comment for "Rendering A Html.erb In Rails With Javascript"