简体   繁体   English

使用JavaScript在Rails中呈现html.erb

[英]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. 但是我想用javascript代码再次渲染这些渲染部分之一。

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: 它的render函数创建了这个:

<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). 您不应该尝试使用javascript 渲染 erb -并不是这样完成的(更不用说您必须编写自己的erb解析器并最终以任何方式调用服务器以获取数据)。

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... 另一方面,如果您要问如何使用Javasript 发布某些模板的渲染 ,然后用新模板替换旧内容,则...

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. 观看此http://railscasts.com/episodes/205-unobtrusive-javascript ,阅读此Rails 3和RJS以了解如何通过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 触发ajax调用(在一段时间后或通过手动用户请求)以向Rails应用程序发出请求
  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) 处理该请求并使用javascript脚本进行响应(脚本内容应包含所有功能和内容以替换您想要的内容)

It's possible to share templates between the server and the client. 可以在服务器和客户端之间共享模板。 One library that allows for this is called mustache . 一种允许这种情况的库称为mustache

But if this is the only case you need it I'd go with the RJS approach as well. 但是,如果这是您唯一需要的情况,那么我也将使用RJS方法。

Instead of rendering I did it AJAX way and update it on the page. 我没有渲染,而是以AJAX方式进行了渲染,并在页面上对其进行了更新。 Thanks for the answers but I did not use any of these. 感谢您的回答,但我没有使用任何这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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