【发布时间】:2014-01-22 04:33:00
【问题描述】:
我想将一些 Rails 模型传递到视图中并渲染它们。
例如
在控制器中
@stories = Story.all
在视图中
$('.story-text').html("#{raw @stories[0].story_text}");
但 HTML 字符串包含 " 和 '。因此 HTML 不完全包含在字符串中,并且 Javascript 无法运行。
例如它变成了
$('.story-text').html("<img src="/example.jpg" />");
这会导致错误。
我怎样才能做到这一点?我可以在 Javascript 中使用像 Python 中的三引号吗?
编辑@stories[0].story_text的示例
<p>This is some example text</p><img src="/example.jpg" />
它以text 类型存储在数据库中
【问题讨论】:
-
请发布您的控制器代码以显示 Story.all 的填充方式。
-
发布
@stories[0].story_text的例子 -
Story.all 通过使用 CKEditor 的表单填充。控制器实际上只是在做
@stories = Story.all我添加了一个示例
标签: javascript html ruby-on-rails haml