我需要帮助在application.html.erb布局文件中的脚本标记内显示rails helper方法的输出。 我在关闭html标记之前放了以下代码:
<script>
App.ready = function() {
App.initApp(jQuery.parseJSON("<%= current_user_json %>"));
}
</script>
此current_user_json是应用程序控制器文件中的辅助方法。
上述代码在浏览器中生成的输出(查看页面源代码)是:
<script>
App.ready = function() {
App.initApp(jQuery.parseJSON("{"id":3,"email":"user5@user.com","username":null}"));
}
</script>
正确的输出应该是:
<script>
App.ready = function() {
App.initApp(jQuery.parseJSON('{"id":3,"email":"user5@user.com","username":null}'))
}
</script>
如果有人可以帮助我,我真的会在过去的几天里解决这个问题。
答案 0 :(得分:2)
试试这个
<%= raw current_user_json.to_json %>
答案 1 :(得分:1)
改变这个:
<%= current_user_json %>
对此:
<%= current_user_json.html_safe %>
首先要确保你的json被正确转义。例如,如果您的current_user_json
恰好有一个带引号的字段,则必须转义该引号。如果你不逃避,那么你正在做的是一个非常典型的黑客攻击媒介,所以继续小心。