简体   繁体   English

将从SQL数据库检索的Rails对象上的ruby存储到javascript对象中

[英]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: 所以我在sql数据库中有一个资产表,并且在我的视图中像这样访问它们:

  <% @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. 我现在要做的是实际上将那些资产属性保存在javascript对象中,以便我可以在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. 我有大量的JavaScript经验,但是Ruby on Rails对我来说还很新。 Any help would be appreciated. 任何帮助,将不胜感激。

If you need something simple you can use to_json , like in the following: 如果您需要简单的东西,可以使用to_json ,如下所示:

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. 因此,您可以使用像spinejs这样的轻量级框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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