in my view i have one object, and want to work with this onject from javascript i try to
var js_obj = jQuery.parseJSON('<%=raw @rails_obj.to_json %>');
it works. but if i have "'" symbols, new string symbols, ,,, in this object all fails.
Have somebody know good approach to do it?
JSON is valid Javascript right out of the box, so why not just do:
var js_obj = <%= @rails_obj.to_json %>;
I find this to be the best way. Worked everytime
<%= javascript_tag "var obj = #{@obj.to_json}" %>
You need to escape all single-quotes then. ActionView has a helper for escaping JavaScript: ActionView::Helpers::JavaScriptHelper#escape_javascript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With