Vishal
Vishal

Reputation: 717

Converting string to hash in rails

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:

"{&quot;id&quot;=&gt;166,&quot;first_name&quot;=&gt;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

Answers (3)

Aniket Rao
Aniket Rao

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

puneet18
puneet18

Reputation: 4427

try

$('#link_id').data('test', <%= @record_obj.as_json.html_safe %>);

Upvotes: 0

Zzz
Zzz

Reputation: 1703

Try use id directly

$('#link_id').data('test', "<%= @record_obj.id %>")

Upvotes: 0

Related Questions