0

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 again

<div id="dialog" title="Bookmark Folders" style="resize: both;">
        <%=render "layouts/filetree.html.erb"%>
</div>

It render function creates this:

<div id="accordion">
    <% @user.folders.each do |f| %> <h3 id="<%= f.folder_name%>" class="folderBar"><a href="#" class="folderName"><b id="folderName"><%= f.folder_name%></b> created <%=distance_of_time_in_words(f.created_at, Time.now)%> before</a></h3>
    <div class="<%= f.folder_name.delete(" ")%>">
        <%if f.docs.empty?%>
        <b>-No document currently!-</b>
        <%else%>
        <% f.docs.each do |r|%>
        <a href="<%=r.url%>" style = "color:#0066CC;"><%= r.title%></a><br/>
        <b><%=r.url%></b><br /> 
        <%= r.snippet%><hr/>
        <%end%>
        <%end%>
    </div>
    <%end%>
</div>

Because after some execution there are some changes over "folders", I want to render this part again to see the update.

erogol
  • 13,156
  • 33
  • 101
  • 155

3 Answers3

0

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)
Community
  • 1
  • 1
WTK
  • 16,583
  • 6
  • 35
  • 45
  • But I guess this example suppose to have page reload after submit, however I dont want to reload the page – erogol Mar 08 '12 at 16:40
  • No, there's no need to relad the page. Do an ajax call, on server side render the template you need and send it as response to that call. Then replace old content with the rendered one. Ease peasy. – WTK Mar 09 '12 at 07:04
0

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.

SpoBo
  • 2,100
  • 2
  • 20
  • 28
0

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.

erogol
  • 13,156
  • 33
  • 101
  • 155