ruby on rails 使用text存储hash类型的数据 Use text filed to store the hash map
来源:互联网 发布:mysql没有密码如何连接 编辑:程序博客网 时间:2024/06/04 19:12
在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, :component_type_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
0 0
- ruby on rails 使用text存储hash类型的数据 Use text filed to store the hash map
- hash map的使用
- 【BZOJ】【P2258】【pku2758 Checking the Text 文本校对】【题解】【hash】
- BZOJ 2258 pku2758 Checking the Text 文本校对 Splay+Hash
- poj 2758: Checking the Text(二分+Hash)
- bzoj 2258: pku2758 Checking the Text 文本校对 hash+二分
- bzoj2258: pku2758 Checking the Text 文本校对 hash+二分
- mysql索引类型Normal,Unique,Full Text区别及索引方法Btree,Hash的区别
- mysql索引类型Normal,Unique,Full Text区别及索引方法Btree,Hash的区别
- hash类型数据的遍历
- Ruby on Rails 入门之:(9) Ruby中Hash对象与数组
- use hash to find dup
- ruby on rails使用
- [Ruby] Ruby 的hash默认值
- The Ruby On Rials Gudie - say hello to rails
- Copy On Write Hash Map
- hash-B-使用在哈希法存储数据
- ruby on rails 的redis 使用
- 【leetcode】389. Find the Difference
- win服务器设置开机自动登录
- Leetcode: Generate Parentheses
- tmux常用命令与快捷键
- C/C++ Volatile关键词深度剖析
- ruby on rails 使用text存储hash类型的数据 Use text filed to store the hash map
- SyntaxError at / Non-ASCII character '\xef' in file D:\Python27\Scripts\HelloWorld\learn\views.py on
- oracle怎样看懂执行计划
- 2 线程的基础知识(中)
- 简单的例子了解View事件分发
- html的模型
- VLFeat和Piotr’s Image & Video Matlab Toolbox在Matlab中的安装与配置
- arm电子相册项目——按键模块篇led.c
- 对象锁、类锁以及synchronized