为什么我的字符串只有在我手动插入时才能起作用?
这是存储在数据库中的确切字符串:
@blog_post.content = "<p><img src=\"http://localhost:3000/assets/sa_clubbing_logo.png\" alt=\"Sa_clubbing_logo\"><div><b>An image</b></div><div><b><br></b></div><div><b>Another image</b></div><div><b><img src=\"http://localhost:3000/assets/sa_clubbing_logo.png\" alt=\"Sa_clubbing_logo\"><br></b></div></p>"
如您所见。它由一些图像和一些粗体文本组成。
好的,所以我将 html 保存在数据库中。我正在尝试将 html 附加到内容可编辑的 div(我的富文本编辑器)中,如下所示。
$(document).ready(function(){
$('#rte').append("<%= @blog_post.content %>");
});
它显示在我的内容可编辑 div 中,但作为实际文本。我希望将其呈现为 html,以便实际显示图像。看起来要附加的 html 是这样的:
<p><p><img src="http://localhost:3000/assets/sa_clubbing_logo.png" alt="Sa_clubbing_logo"><div><b>An image</b></div><div><b><br></b></div><div><b>Another image</b></div><div><b><img src="http://localhost:3000/assets/sa_clubbing_logo.png" alt="Sa_clubbing_logo"><br></b></div></p></p>
所以我尝试手动将字符串放入附加方法中,我得到了我想要的:
$(document).ready(function(){
$('#rte').append("<p><img src=\"http://localhost:3000/assets/sa_clubbing_logo.png\" alt=\"Sa_clubbing_logo\"><div><b>An image</b></div><div><b><br></b></div><div><b>Another image</b></div><div><b><img src=\"http://localhost:3000/assets/sa_clubbing_logo.png\" alt=\"Sa_clubbing_logo\"><br></b></div></p>");
});
这里附加的 html 是这样的:
<p><img src="http://localhost:3000/assets/sa_clubbing_logo.png" alt="Sa_clubbing_logo"></p><div><b>An image</b></div><div><b><br></b></div><div><b>Another image</b></div><div><b><img src="http://localhost:3000/assets/sa_clubbing_logo.png" alt="Sa_clubbing_logo"><br></b></div><p></p>
图像以这种方式显示在我的编辑器中我想他们应该这么做。
为什么当我手动插入字符串时此功能有效,但当我使用 <%= @blog_posts.content %>
时则无效?
我一定在这里遗漏了某种格式问题。
Here is the exact string that is stored in the db:
@blog_post.content = "<p><img src=\"http://localhost:3000/assets/sa_clubbing_logo.png\" alt=\"Sa_clubbing_logo\"><div><b>An image</b></div><div><b><br></b></div><div><b>Another image</b></div><div><b><img src=\"http://localhost:3000/assets/sa_clubbing_logo.png\" alt=\"Sa_clubbing_logo\"><br></b></div></p>"
as you can see. It is made up of a few images and some bold text.
Ok, so I have html saved in the db. I am trying to append the html to a content editable div (my rich text editor) like this.
$(document).ready(function(){
$('#rte').append("<%= @blog_post.content %>");
});
It is showing up in my content editable div, but as actual text. I want it to be rendered as html so the images are actually showing. It seems like the html that is being appended is this:
<p><p><img src="http://localhost:3000/assets/sa_clubbing_logo.png" alt="Sa_clubbing_logo"><div><b>An image</b></div><div><b><br></b></div><div><b>Another image</b></div><div><b><img src="http://localhost:3000/assets/sa_clubbing_logo.png" alt="Sa_clubbing_logo"><br></b></div></p></p>
So I tried putting the string into the append method manually and I got exactly what I wanted:
$(document).ready(function(){
$('#rte').append("<p><img src=\"http://localhost:3000/assets/sa_clubbing_logo.png\" alt=\"Sa_clubbing_logo\"><div><b>An image</b></div><div><b><br></b></div><div><b>Another image</b></div><div><b><img src=\"http://localhost:3000/assets/sa_clubbing_logo.png\" alt=\"Sa_clubbing_logo\"><br></b></div></p>");
});
The html being appended here is this:
<p><img src="http://localhost:3000/assets/sa_clubbing_logo.png" alt="Sa_clubbing_logo"></p><div><b>An image</b></div><div><b><br></b></div><div><b>Another image</b></div><div><b><img src="http://localhost:3000/assets/sa_clubbing_logo.png" alt="Sa_clubbing_logo"><br></b></div><p></p>
The images are showing up in my editor just the way I imagined they should.
Why does this work when I insert the string manually, but not when I use <%= @blog_posts.content %>
?
I must be missing some sort of formatting issue here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换:
为:
或:
甚至:
说明:
Rails 默认情况下会保护您,因此您必须告诉您何时认为 html 是安全的。
Replace:
With:
Or:
Or even:
Explanation:
Rails protects you by default so you have to tell when you consider html safe.