简体   繁体   English

从rails中的jquery保存一个哈希数组

[英]Saving an array of hashes from jquery in rails

I'm a newbie to RoR. 我是RoR的新手。 The problem I am trying to solve is how to pass an array of hashes from JQuery to Rails for saving in the database. 我试图解决的问题是如何将一个哈希数组从JQuery传递给Rails以保存在数据库中。

Context 上下文

In my application, a user is able to drag-and-drop any number of items onto a canvas. 在我的应用程序中,用户可以将任意数量的项目拖放到画布上。 This is done using JQuery. 这是使用JQuery完成的。

Then the user can save these items, including their XY coordinates. 然后用户可以保存这些项目,包括它们的XY坐标。

Conceptually I believe that this is saving an array of hashes, where each hash corresponds to an item and consists of an ItemCode, X-coordinate, Y-coordinate. 从概念上讲,我相信这会保存一个哈希数组,其中每个哈希对应一个项目,由一个ItemCode,X坐标,Y坐标组成。

I am trying to figure out at each step, what method/technique should be used. 我试图弄清楚每一步应该使用什么方法/技术。

Here's what I figured out so far: 这是我到目前为止所发现的:

  1. ? Save an array of objects in JQuery ? 在JQuery中保存一组对象?
  2. Serialize this array using jquery.param 使用jquery.param序列化此数组
  3. Pass the resulting string as params to Rails using jquery.ajax 使用jquery.ajax将生成的字符串作为参数传递给Rails
  4. ?? ?? Rails controller somehow parses this string and saves it in the database?? Rails控制器以某种方式解析这个字符串并将其保存在数据库中?

Key Questions 关键问题

A. What format should this serialized string take to be easily parsed by Rails? 答:这个序列化字符串应采用什么格式才能轻松解析?

I believe Jquery.param will generate something like this (in param form): 1[item_code]=432&1[x]=250&1[y]=200&2[item_code]=348... I haven't been able to find anything to tell me if Rails can figure out that the first 3 terms describe 1 item, the next 3 describe the next item, etc. 我相信Jquery.param会生成这样的东西(以param形式):1 [item_code] = 432&1 [x] = 250&1 [y] = 200&2 [item_code] = 348 ...我找不到任何东西告诉我Rails是否可以确定前3个术语描述1个项目,接下来的3个术语描述下一个项目,等等。

B. What is the Rails way to handle this? B. Rails处理这个问题的方法是什么? In other words, what particular methods should I look at as my tools for putting this params string into the database as a several new entries into the database? 换句话说,我应该将哪些特定方法看作是将这个params字符串作为数据库中的几个新条目放入数据库的工具?

And of course, if there's some completely different way to do this, I'd love to hear it. 当然,如果有一些完全不同的方式,我很乐意听到它。 Thanks. 谢谢。

EDIT 编辑

Just to clarify, the ItemCode is meant to be different from the id of the item entry into the database. 只是为了澄清,ItemCode意味着与项目条目的id不同。 ItemCode is used to look up characteristics about the item in other tables. ItemCode用于查找其他表中项目的特征。

One way: 单程:

decodeURIComponent(jQuery.param({ items: { item1: { x: 1, y: 2 }, item2: { x: 3, y: 4 } } } ))  #  => items[item][x]=1&items[item][y]=2&items[item2][x]=3&items[item2][y]=4

yields a params hash that looks like 产生一个看起来像的params哈希

{"items"=>{"item"=>{"x"=>"1", "y"=>"2"}, "item2"=>{"x"=>"3", "y"=>"4"}}}

Then in the controller: 然后在控制器中:

params[:items].each do |k,v|
  begin
    item = Item.new( id: k, x: v.x, y: v.y )  # or better, Item.new( x: v.x, y: v.y ) 
    item.save
  rescue ActiveRecord::RecordInvalid # deal with problems saving
    ...
    # retry?
  end
end

In your question, you are generating item ID's in the client. 在您的问题中,您正在客户端生成项目ID。 It's a better idea to let the DB handle that when you are actually creating the objects to eliminate the need to keep track of unique ID's. 当你实际创建对象时,让DB处理它是一个更好的主意,以消除跟踪唯一ID的需要。 In that case, the ID's passed to the controller are just arbitrary placeholders, but they do have to be unique in each .param() call to distinguish one object from another. 在这种情况下,传递给控制器​​的ID只是任意占位符,但在每个.param()调用中它们必须是唯一的,以区分一个对象。

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

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