如何在单列中保存多个复选框的值 [英] How to save the value of multiple checkboxes in a single column

查看:23
本文介绍了如何在单列中保存多个复选框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为配置文件的表,其中有几列:地址、电话、语言.我希望能够在语言列中保存多种语言,例如:英语、德语、西班牙语,并且能够从配置文件/节目视图中分别访问每种语言.在配置文件/新视图中,我将使用复选框选择语言.我不能做的是在控制器中的参数中,例如保存在数据库中我的观点:

<h4>习语:</h4><div class="form-check form-check-inline"><%= f.check_box :es, class:'form-check-input' %><%= f.label :es, "Español";%>

<div class="form-check form-check-inline"><%= f.check_box :en, class:'form-check-input' %><%= f.label :en, "Inglés";%>

<div class="form-check form-check-inline"><%= f.check_box :pt, class:'form-check-input' %><%= f.label :pt, "Portugués";%>

<div class="form-check form-check-inline"><%= f.check_box :fr, class:'form-check-input' %><%= f.label :fr, Francés";%>

<div class="form-check form-check-inline"><%= f.check_box :it, class:'form-check-input' %><%= f.label :it, Italiano";%>

<div class="form-check form-check-inline"><%= f.check_box :ge, class:'form-check-input' %><%= f.label :ge, "Alemán";%>

解决方案

你可以使用 位域 将一系列位表示您选择的语言作为数据库中的单个列,有一些 gems 支持 bit field(https://www.ruby-toolbox.com/categories/Active_Record_Bit_Fields).

带有 flag_shih_tzu gem 的演示

#迁移add_column :profiles, :flags, :integer, :null =>假,:默认=>0# flag_shih_tzu 管理的位域# 将存储在标志列中的有效布尔值:# t.boolean :en# t.boolean :es# t.boolean :fr# 等等 ...# 模型/profile.rb班级简介包括FlagShihTzuhas_flags 1 =>:zh,2 =>:es,3 =>:fr结尾个人资料 = Profile.newprofile.en = 真profile.es = 真配置文件保存个人资料 = 个人资料.last简介.en?# 真的简介.es?# 真的简介.fr?# 错误的

这样您就可以从视图中选中的复选框中为所选语言设置 true.

更新如果您不想使用上面的那些 bitfield gem,这里是 postgres 位串(或 mysql binary-varbinary),因此您的语言设置将作为位序列缓存在数据库中,如01010011",1:选中,0:无.

create_table :profiles, force: true do |t|t.column:语言,位(8)";#位(16)";任何 ...结尾班级简介申请记录before_create :set_default_languages# 你可以随心所欲地重构[:ruby, :python, :js, :java, ...].each_with_index do |lang, index|define_method("#{lang}?") 做结果 = ActiveRecord::Base.connection.execute("选择 get_bit(languages, #{index})来自个人资料其中 id = #{self.id} 限制 1")结果&.first.values&.first == 1结尾define_method("#{lang}!") 做ActiveRecord::Base.connection.execute("更新配置文件 SET 语言 =set_bit(languages, #{index}, 1) where id = #{self.id}")结尾结尾私人的def set_default_languagesself.languages = "00000000";结尾结尾profile = Profile.create(...)profile.ruby?# 错误的profile.ruby!# 设置为 1profile.reloadprofile.ruby?# 真的

I have a table called profiles, and in which I have several columns: address, phone, languages. I would like to be able to save in the languages ​​column, several languages ​​for example: English, German, Spanish and to be able to access each language separately from the profiles / show view. In the profiles / new view, I would select the languages ​​using the checkbox. what I can't do is in the controller in the parameters like saving in the database my view:

<div class="col-xl-8">
  <h4>Idiomas:</h4>
    <div class="form-check form-check-inline">
    <%= f.check_box :es, class:'form-check-input' %>
    <%= f.label :es, "Español" %>
    </div>

    <div class="form-check form-check-inline">
    <%= f.check_box :en, class:'form-check-input' %>
    <%= f.label :en, "Inglés" %>
    </div>
    
    <div class="form-check form-check-inline">
    <%= f.check_box :pt, class:'form-check-input' %>
    <%= f.label :pt, "Portugués" %>
    </div>

    <div class="form-check form-check-inline">
    <%= f.check_box :fr, class:'form-check-input' %>
    <%= f.label :fr, "Francés" %>
    </div>

    <div class="form-check form-check-inline">
    <%= f.check_box :it, class:'form-check-input' %>
    <%= f.label :it, "Italiano" %>
    </div>

    <div class="form-check form-check-inline">
    <%= f.check_box :ge, class:'form-check-input' %>
    <%= f.label :ge, "Alemán" %>
    </div>

</div>    

解决方案

you could use a bit field to hold a sequence of bits which represent your choice languages as a single column in database, there's some gems support bit field(https://www.ruby-toolbox.com/categories/Active_Record_Bit_Fields).

demo with flag_shih_tzu gem

# migration
 add_column :profiles, :flags, :integer, :null => false, :default => 0
 # flag_shih_tzu-managed bit field
 # Effective booleans which will be stored on the flags column:
 # t.boolean      :en
 # t.boolean      :es
 # t.boolean      :fr
 # and so on ...

# models/profile.rb
class Profile
 include FlagShihTzu
 has_flags 1 => :en,
           2 => :es,
           3 => :fr
end

profile = Profile.new
profile.en = true
profile.es = true
profile.save

profile = Profile.last
profile.en? # true
profile.es? # true
profile.fr? # false

so you could set true for selected languages from your view checked check-boxes.

update in case you don't want to use those bitfield gems above, here is demo with postgres bitstring (or mysql binary-varbinary), so your languages setting be cached on database as bit sequence like this "01010011", 1: selected, 0: none.

create_table :profiles, force: true do |t|
  t.column :languages, "bit(8)" # "bit(16)" whatever ...
end

class Profile < ApplicationRecord
 before_create :set_default_languages
 
 # you could refactor as you want
 [:ruby, :python, :js, :java, ...].each_with_index do |lang, index|
    define_method("#{lang}?") do
      result = ActiveRecord::Base.connection.execute(
      "Select get_bit(languages, #{index}) 
        From profiles Where id = #{self.id} Limit 1")

      result&.first.values&.first == 1
    end

    define_method("#{lang}!") do
      ActiveRecord::Base.connection.execute(
      "Update profiles SET languages = 
        set_bit(languages, #{index}, 1) Where id = #{self.id}")
    end
  end

 private
 
  def set_default_languages
    self.languages = "00000000"
  end
end

profile = Profile.create(...)
profile.ruby? # false
profile.ruby! # set to 1
profile.reload
profile.ruby? # true

这篇关于如何在单列中保存多个复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆