在component表里用text类型的字段存储hash数据

(1)新建字段 ,这是migration的内容

class AddHintsToComponents < ActiveRecord::Migration[5.0]
def change
add_column :components, :hints, :text
end
end

(2)controller

  def update
if @component.update!(component_params)
redirect_to @component, notice: 'Component was successfully updated.'
else
render :error
end
end
    def component_params
params.require(:component).permit(:release_id,
:remark, component_ids: [], hints: [:left, :mid, :right])
end

(3)model

class Component < ApplicationRecord
serialize :hints def after_initialize
self.hints ||= {}
end

(4)_component.html.erb

  <td>
left:<%= component.hints["left"] %> |
mid:<%= component.hints["mid"] %> |
right:<%= component.hints["right"] %>
</td

(5)_form.html.erb

  <%= f.fields_for :hints do |h| %>
<div class="form-group">
<%= f.label 'hints left', class: 'col-sm-2 control-label' %>
<div class="col-sm-4">
<%= h.text_field :left, class: 'form-control',
value: (@component.hints && @component.hints.key?("left") ? @component.hints["left"] : "") %>
</div>
</div> <div class="form-group">
<%= f.label 'hints mid', class: 'col-sm-2 control-label' %>
<div class="col-sm-4">
<%= h.text_field :mid, class: 'form-control',
value: (@component.hints && @component.hints.key?("mid") ? @component.hints["mid"] : "") %>
</div>
</div> <div class="form-group">
<%= f.label 'hints right', class: 'col-sm-2 control-label' %>
<div class="col-sm-4">
<%= h.text_field :right, class: 'form-control',
value: (@component.hints && @component.hints.key?("right") ? @component.hints["right"] : "") %>
</div>
</div>
<% end %>

参考资料

http://vladexologija.blogspot.com/2012/12/rails-attribute-serialization_18.html

http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters

使用text存储hash类型的数据 Use text filed to store the hash map的更多相关文章

  1. 如何使用Cassandra来存储time-series类型的数据

       Cassandra非常适合存储时序类型的数据,本文我们将使用一个气象站的例子,该气象站每分钟需要存储一条温度数据. 一.方案1,每个设备占用一行      这个方案的思路就是给每个数据源创建一行 ...

  2. Redis 笔记与总结2 String 类型和 Hash 类型

    Linux 版本信息: cat /etc/issue 或cat /etc/redhat-release(Linux查看版本当前操作系统发行版信息) CentOS release 6.6 (Final) ...

  3. Perl中的hash类型

    hash类型 hash类型也称为字典.关联数组.映射(map)等等,其实它们都是同一种东西:键值对.每一个Key对应一个Value. hash会将key/value散列后,按序放进hash桶.散列后的 ...

  4. Redis中Value使用hash类型的效率是普通String的两倍

    什么Redis? 点击这里 最近要开发的一个项目是分布式缓存组件,解决参数缓存高效获取的问题.参数达到了500万级别,刚刚开始了解Redis.做设计的时候考虑到Value使用哪种类型的问题? 主要面临 ...

  5. redis之Hash类型常用方法总结

    redis之Hash类型常用方法总结 格式: 存--HMGET key field [field ...] 取--HMGET key field [field ...] M:表示能取多个值,many ...

  6. Hashtable数据存储结构-遍历规则,Hash类型的复杂度为啥都是O(1)-源码分析

    Hashtable 是一个很常见的数据结构类型,前段时间阿里的面试官说只要搞懂了HashTable,hashMap,HashSet,treeMap,treeSet这几个数据结构,阿里的数据结构面试没问 ...

  7. 直接在安装了redis的Linux机器上操作redis数据存储类型--hash类型

    一.概述:   我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息.如Username.Password和 ...

  8. 关于InnoDB存储引擎text和blob类型的优化

    我们在数据库优化的时候,看到一些表在设计上使用了text或者blob的字段,如果单表的存储空间达到了近上百G或者大几十G,这种情况再去改变和优化就非常难了 一.简介 为了清楚大字段对性能的影响,我们有 ...

  9. StackExchange.Redis帮助类解决方案RedisRepository封装(散列Hash类型数据操作)

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明本系列分享地址:http://www.cnblogs.com/tdws/p/5815735.html 上一篇文章的不合理之处,已经有所修改. 今天分 ...

随机推荐

  1. 一种M2M业务的架构及实现M2M业务的方法

    http://www.cnblogs.com/coryxie/p/3849764.html 技术领域 [0001] 本发明涉及通信技术领域,尤其涉及一种M2M业务的架构及实现M2M业务的方法. 背景技 ...

  2. Python实现打印二叉树某一层的所有节点

    不多说,直接贴程序,如下所示 # -*- coding: utf-8 -*- # 定义二叉树节点类 class TreeNode(object): def __init__(self,data=0,l ...

  3. Java类集

    类集就是一个动态的对象数组,是对一些实现好的数据结构进行了包装,这样在使用时就会非常方便,最重要的是类集框架本身不受对象数组长度的限制. 类集框架的主要接口

  4. 9-slice-scaling

    9-slice-scaling http://rwillustrator.blogspot.com/2007/04/understanding-9-slice-scaling.html

  5. Jquerymobile随笔

    fixed <div data-role="header" data-position="fixed"> <h1>欢迎访问我的主页< ...

  6. mysqlbinlog恢复数据-update20140820

    mysqlbinlog恢复数据 BINLOG就是一个记录SQL语句的过程,和普通的LOG一样.只是它是二进制存储,普通的是十进制存储. ================================ ...

  7. 解决umount.nfs: /data: device is busy 问题

    有时候我们需要umount某个挂载目录时会遇到如下问题: [root@localhost /]# umount /data/ umount.nfs: /data: device is busy 通过这 ...

  8. OC-改错题

    1,类方法中不能访问成员变量 2,id后不能加*(因为id相当于NSObject *) 3,id类型的变量不能用点语法 4,类对象只能调用类方法,不能调用对象方法 .description #impo ...

  9. NSOperation GCD 对比 (附NSOperation演练)

    项目中使用NSOperation的优点是NSOperation是对线程的高度抽象,在项目中使用它,会使项目的程序结构更好子类化NSOperation的设计思路,是具有面向对象的优点(复用.封装),使得 ...

  10. 多线程 GET

    iOS中多线程的实现 方案 简介 语言 线程生命周期 使用频率 pthread 一套通用的多线程API 适用于 Unix / Linux / Windows 等系统 跨平台\可移植 使用难度大 C 程 ...