samedi 6 août 2016

Convert instance object into JS object in "view" in order to assign into data- html attribute cuts off in middle

I am looping instance variable "@packages" set in controller and am trying to assign single object into one of the html element by using "data-". For changing ruby object into json i am using ".to_json" as follows:

<% @packages.each do |package| %>
    <% json_package=package.to_json %>
     <a href="#" data-single-package=<%= json_package %>></a>
<% end %>

However, on inspecting the anchor tag i found json object is cutting off in one of the property which had a value with space in between it.

enter image description here

As you can see, "title" property has value "annarpurna circuit", but ".to_json" is cutting off in space by adding ".

How can i make this whole string pass through??

Aucun commentaire:

Enregistrer un commentaire