我需要幫助在application.html.erb佈局文件的腳本標記中顯示rails helper方法的輸出。 我把下面的代碼在結束HTML標記之前:Ruby on Rails在應用程序中嵌入Ruby代碼Erb文件
<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":"[email protected]","username":null}"));
}
</script>
正確的輸出應該是:
<script>
App.ready = function() {
App.initApp(jQuery.parseJSON('{"id":3,"email":"[email protected]","username":null}'))
}
</script>
如果有人可以幫助我,我真的會解決這個問題我想在過去幾天解決。
不'<%= current_user_json.html_safe%>'工作? – spickermann
嗨@spickermann您的回答是正確的! – codingbear