热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为哈希属性创建表单字段-Createformfieldsforhashattribute

IhaveamodelProduct,whichhasapropertiesattribute.Itstoresitinasingledatabasecolumna

I have a model Product, which has a properties attribute. It stores it in a single database column as a hash (following http://api.rubyonrails.org/classes/ActiveRecord/Store.html)

我有一个模型Product,它有一个属性属性。它将它作为哈希存储在单个数据库列中(在http://api.rubyonrails.org/classes/ActiveRecord/Store.html之后)

class Product 

How can I create dynamic form fields for this property attribute (which is a hash)? I'm interested in ideologically correct way of doing this ("rails way"). I guess that there is need to use fields_for helper. But I do not fully understand how to do it. Tell me the correct way of solving this problem, please.

如何为此属性属性(这是一个哈希)创建动态表单字段?我对意识形态正确的做法感兴趣(“轨道方式”)。我想有需要使用fields_for帮助器。但我不完全明白该怎么做。请告诉我解决这个问题的正确方法。

In result, I'd like to get a working form like shown on image.

结果,我想得到一个像图像所示的工作表格。

enter image description here

Where a user can add unlimited number of fields and give any property names and its values.

用户可以添加无限数量的字段并提供任何属性名称及其值。

3 个解决方案

#1


8  

The rails way needn't include the limitation of using a single table, ideally, you can do this in a very rails way with 2 tables. Moving on.

rails方式不需要包括使用单个表的限制,理想情况下,您可以使用2个表以非常有轨的方式执行此操作。继续。

You shouldn't use Active Record :store for this purpose in my opinion.

在我看来,您不应该使用Active Record:store来实现此目的。

That implementation is ideal for situations where the developers need to store model metadata that is flexible in-code but well-defined at any given point of time. Which is to say, you need to specify keys in the model.

对于开发人员需要存储灵活的代码但在任何给定时间点定义良好的模型元数据的情况,该实现非常理想。也就是说,您需要在模型中指定键。

There is another pit-fall, you can't run SQL queries on the resulting serialized text that is saved.

还有另一个陷阱,您无法对保存的结果序列化文本运行SQL查询。

If you insist, you can do this:

如果你坚持,你可以这样做:

In your model:

在你的模型中:

class Product 

In the view:

在视图中:

<%= form_for @product do |f| %>
  <% f.object.prop_hash.each do |k,v| %>
    <%= text_field 'product[prop_hash][][name]', k %>
    <%= text_field 'product[prop_hash][][value]', v %>
  <% end %>
<% end %>

Then you can also add an 'add another property' link which should use JS to insert another pair of inputs with the names product[prop_hash][][name] and product[prop_hash][][value] respectively.

然后你还可以添加一个'添加另一个属性'链接,该链接应该使用JS分别插入名为product [prop_hash] [] [name]和product [prop_hash] [] [value]的另一对输入。

Long ago I had rolled a custom implementation for metadata which saves the keys in serialized XML, for one reason - it can be queried in SQL. The link to my blog article http://geniitech.tumblr.com/post/14916592782/storing-metadata-as-xml-hash-in-ror

很久以前,我已经推出了元数据的自定义实现,它将密钥保存在序列化的XML中,原因之一是 - 它可以在SQL中查询。我的博客文章http://geniitech.tumblr.com/post/14916592782/storing-metadata-as-xml-hash-in-ror的链接

#2


1  

To be brief, you want to have a form that contains this within it somewhere:

简而言之,您希望在某个地方有一个包含此内容的表单:

<% @product.properties_hash.each do |k,v| %>
  <%= f.field_for k %>
  <%= f.field_for v %>
  <# link to action that will remove this key/value pair from the serialized hash saved in the database %>
<% end -%>

You're going to have to create a blank key and value pair somehow (either here in the form just using the form helpers or by adding a blank(ish) key and value pair to the end of the hash itself by modifying the product in the controller after you load it into @product (like with a @product.add_blank_properties method). Otherwise you won't have 'blank' fields at the end of the loop. . .

你将不得不以某种方式创建一个空白键和值对(在这里只是使用表单助手,或者通过在哈希本身的末尾添加一个空白(ish)键和值对来修改产品将控制器加载到@product后(就像使用@ product.add_blank_properties方法一样)。否则在循环结束时你不会有'空白'字段。

I could go into more detail about adding a new blank line for a property after you create one using the previously existing blank line, but by the time you get this far you should have a good grasp on what you need to look for to solve that (and there are plenty of resources out there (you will probably be using ajax for this).

在使用以前存在的空白行创建属性之后,我可以详细介绍如何为属性添加新的空白行,但是当你到达目前为止,你应该很好地掌握你需要寻找什么来解决这个问题。 (并且有很多资源(你可能会使用ajax)。

#3


-2  

I think you should be using rails standard scaffold but here you may need few customization's.

我认为你应该使用rails标准脚手架,但在这里你可能需要很少的定制。

1) Need to declare resourceful route for properties property name will become resource id 2) Index view will iterate over properties hash to populate relevant fields.

1)需要为属性声明资源路由属性名称将成为资源ID 2)索引视图将迭代属性哈希以填充相关字段。

product.properties.each do |name, value|
   puts name
   puts value
end

2) Create action should create a record if doesn't exist (based on name) already otherwise give an error

2)创建操作应该创建一个记录,如果不存在(基于名称)已经否则会给出错误

product.properties[:price] = 10

3) Update will find property based on name and update it hash way

3)更新将根据名称查找属性并更新其哈希方式

product.properties[:price] = 25

4) delete will actually delete the key-value pair from hash based on property name

4)delete实际上会根据属性名从哈希中删除键值对

product.properties.delete(:price)

推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • vue使用
    关键词: ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 2019独角兽企业重金招聘Python工程师标准
    本文介绍了2019独角兽企业对Python工程师的招聘标准,包括在AndroidManifest中定义meta-data的方法和获取meta-data值的代码。同时提供了获取meta-data值的具体实现方法。转载文章链接:https://my.oschina.net/u/244918/blog/685127 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
author-avatar
单莼de笑脸
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有