Reputation: 717
I need to convert an string to hash so that i can use its "id" attribute. I am getting this string due to following code in my .js.erb file:
$('#link_id').data('test', "<%= @record_obj.as_json %>");
I am getting string in following format:
"{"id"=>166,"first_name"=>James}"
I need to convert above string to hash so that i can access "id" of the object.
Any help will be appreciated. Thanks :)
Upvotes: 0
Views: 1930
Reputation: 778
Add 'raw' to get a clean output when using erb inside of javascript.
$('#link_id').data('test', JSON.parse(<%= raw @record_obj.as_json %>));
Upvotes: 1
Reputation: 4427
try
$('#link_id').data('test', <%= @record_obj.as_json.html_safe %>);
Upvotes: 0
Reputation: 1703
Try use id directly
$('#link_id').data('test', "<%= @record_obj.id %>")
Upvotes: 0