简体   繁体   中英

Storing ruby on rails objects retrieved from sql database into javascript objects

So I have an assets table in my sql database and I am accessing them in my view like so:

  <% @step.assets.each do |asset| %>
    <td><%= asset.name%></td>
    <td><%= asset.width%></td>
    <td><%= asset.height%></td>
    <td><%= asset.x%></td>
    <td><%= asset.y%></td>
  <% end -%>

And as you can see displaying the different attributes inside a table for the user to see.

The thing that I want to do now is actually save those asset attributes in javascript objects so that I can manipulate them in the ui. I tried something like:

    var obj = <%= @step.asset.name%>; 

But that didn't work and I have searched around for a couple hours now looking for a solution. I have a ton of experience with javascript but Ruby on Rails is still pretty new to me. Any help would be appreciated.

If you need something simple you can use to_json , like in the following:

var asset = <%= raw @step.asset.to_json %>

By the way, it's not a very maintainable solution. So you could use a lightweight framework like spinejs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM