简体   繁体   中英

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 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.

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)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM