Rails3:如何让 js.erb 将 HTML 插入到您的页面中
我有一个 js.erb 视图,它将替换我页面上的某些内容。我希望它插入 HTML,但它插入的 HTML 未正确呈现 - 您可以看到 html 标签。
我尝试了很多我能想到的方法,甚至尝试了其他人推荐的更多方法,但没有一个有效。例如,在我的 js.erb 中:
var list = $("#list");
list.append("<li>first</li>");
list.append("<%= "<li>second</li>" %>");
list.append("<%= raw("<li>third</li>") %>");
list.append("<%= escape_javascript raw("<li>fourth</li>") %>");
list.append("<%= raw escape_javascript("<li>fifth</li>") %>");
list.append("<%= escape_javascript("<li>sixth</li>") %>");
list.append("<%= "<li>seventh</li>".html_safe %>");
list.append("<%= raw("<li>eigth</li>").html_safe %>");
list.append(unescape("<li>9th</li>"));
list.append(unescape("<%= raw("<li>10th</li>") %>"));
list.append(unescape(unescape("<li>11th</li>")));
这就是我附加到列表中的内容(请注意,这是直接从 HTML 页面复制的,而不是页面的源代码):
<li>first</li><li>second</li><li>third</li><li>fourth</li><li>fifth</li><li>sixth</li><li>seventh</li><li>eigth</li><li>9th</li><li>10th</li><li>11th</li>
如何我应该这样做以便页面将其呈现为 HTML 吗?
谢谢!
注意:我正在将我的应用程序从 Rails 2 升级到 Rails 3,这是我在升级时遇到的问题。它在 Rails 2 中运行良好,这就是为什么我认为这是 js.erb 问题而不是 JavaScript 问题。
I have a js.erb view, and it will replace certain things on my page. I want it to insert HTML but the HTML it inserts isn't rendered properly -- you can see the html tags.
I've tried numerous methods I could imagine, and even more that others have recommended, but none work. For example, in my js.erb:
var list = $("#list");
list.append("<li>first</li>");
list.append("<%= "<li>second</li>" %>");
list.append("<%= raw("<li>third</li>") %>");
list.append("<%= escape_javascript raw("<li>fourth</li>") %>");
list.append("<%= raw escape_javascript("<li>fifth</li>") %>");
list.append("<%= escape_javascript("<li>sixth</li>") %>");
list.append("<%= "<li>seventh</li>".html_safe %>");
list.append("<%= raw("<li>eigth</li>").html_safe %>");
list.append(unescape("<li>9th</li>"));
list.append(unescape("<%= raw("<li>10th</li>") %>"));
list.append(unescape(unescape("<li>11th</li>")));
And this is what I get appended to the list (note that this is copied straight from the HTML page, not the source of the page):
<li>first</li><li>second</li><li>third</li><li>fourth</li><li>fifth</li><li>sixth</li><li>seventh</li><li>eigth</li><li>9th</li><li>10th</li><li>11th</li>
How do I do this so that the page will render it as HTML?
Thanks!
Note: I'm in the process of upgrading my app from Rails 2 to Rails 3, and this is something I've run into while upgrading. It worked fine in Rails 2 that's why I think this is a js.erb issue and not a JavaScript issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以你明白这个答案,请阅读我的问题的评论。它解释了我正在使用 ajaxForm。
简而言之,我认为 Rails 3 给出的响应的 mime 类型与 Rails 2 不同。我怀疑这就是答案,因为在谷歌搜索解决方案时,我发现了remotipart gem。该 gem 可帮助开发人员使 Rails 3 远程文件上传变得“微不足道”。
我注意到自述文件状态中的指令将 javascript 包装在 remotipart_response 块中。 查看 remotipart.rb 的第 3 行 我看到mime 类型更改为 HTML。
你瞧,我实现了 gem,一切又恢复正常了。
So you understand this answer, read the comments of my question. It explains that I was using ajaxForm.
In short, I think the response's mime type given back by Rails 3 isn't the same as Rails 2. I suspect this is the answer because while Googling for solutions, I found the remotipart gem. That gem helps developers make Rails 3 remote file uploads "trivial".
I noticed that the instructions in the README state to wrap the javascript in a remotipart_response block. Looking at line 3 of remotipart.rb I see that the mime type is changed to HTML.
Lo and behold, I implemented the gem and things work again.
试试这个...
或者,如果您渲染部分
Try this...
OR,In case you render a partial