How to deal with HTML entities in Rails to_json output?

后端 未结 2 803
终归单人心
终归单人心 2021-01-18 04:16

I\'m writing an app that uses Rails on the backend and javascript/backbone on the frontend. I\'m trying to bootstrap some rails models into my javascript. Specifically, I\'

相关标签:
2条回答
  • 2021-01-18 04:41

    The question is solved by my comment, just for the record:

    Rails escapes strings that are printed using <%= 'string' %>. By this, it is save to ouput user data. So, if you don't want Rails to escape the output, you have to tell Rails explicitly by using raw('string').

    In your code, that would be: <%= raw(@courses.to_json) %>

    0 讨论(0)
  • 2021-01-18 04:52

    If you intend to use raw(obj.to_json) you MUST ensure the following is set.

    ActiveSupport.escape_html_entities_in_json = true 
    
    0 讨论(0)
提交回复
热议问题