mysql - 哈希未插入数据库

标签 mysql ruby-on-rails ruby

我正在尝试将数组插入数据库。我可以在参数中看到数组 (status"=>{"group_one"=>"One", "group_two"=>"One"}),但在插入 inot db 时,它被忽略。

我的控制台显示以下内容:

Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"JgCBl8vcDAP9AUGaylk34om4mbKkxHCgM+GuAfxTL3sywaJkmvKL3pyS2C44MAkzMZ6AK+CUVv/Vmg9aUQYrZw==", 
"irb"=>{"proposalno"=>"", "otherapptype"=>"", "titleofproject"=>"",
 "date1"=>"", "date2"=>"", "date3"=>"", "fundingdetails"=>"", 
"fund"=>"No internal funds or external funds are requested", 
"datetobegindc"=>"", "rationale"=>"", "abstract"=>"", 
"noofsub"=>"0", "natureofassociation"=>"", 
"subjectselection"=>"", "confidentiality"=>"", 
"howwhereconsent"=>"", "methodproceduresubjectparti"=>"",
"childrenpermission"=>"", "infowithheld"=>"", 
"riskbenefitratio"=>"", "minimizingrisk"=>""}, 
"status"=>{"group_one"=>"One", "group_two"=>"One"}, 
"responsibility"=>{"nameoffac"=>"", "nameofinv"=>"", 
"deptoffac"=>"", "deptofinv"=>"", "addoffac"=>"", 
"addofinv"=>"", "phoneoffac"=>"", "phoneofinv"=>"", 
"emailoffac"=>"", "emailofinv"=>""}, "commit"=>"SUBMIT MY DETAILS"}
   (0.2ms)  begin transaction
  SQL (2.9ms)  INSERT INTO "irbs" ("proposalno", "titleofproject",
 "date1", "date2", "date3", "fund", "fundingdetails", "datetobegindc",
 "rationale", "abstract", "noofsub", "natureofassociation",
 "subjectselection", "confidentiality", "howwhereconsent", 
"methodproceduresubjectparti", "childrenpermission", "infowithheld",
 "riskbenefitratio", "minimizingrisk", "otherapptype", "created_at", 
"updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
 ?, ?, ?, ?, ?, ?)  [["proposalno", ""], ["titleofproject", ""], 
["date1", ""], ["date2", ""], ["date3", ""], 
["fund", "No internal funds or external funds are requested"],
 ["fundingdetails", ""], ["datetobegindc", ""], ["rationale", ""], 
["abstract", ""], ["noofsub", "0"], ["natureofassociation", ""],
 ["subjectselection", ""], ["confidentiality", ""], 
["howwhereconsent", ""], ["methodproceduresubjectparti", ""], 
["childrenpermission", ""], ["infowithheld", ""], 
["riskbenefitratio", ""], ["minimizingrisk", ""], 
["otherapptype", ""], ["created_at", 2016-10-18 19:48:35 UTC], 
["updated_at", 2016-10-18 19:48:35 UTC]]

这是我的 Controller :

class IrbsController < ApplicationController

  def new
    @irb = Irb.new
  end



  def create
     @irb = Irb.new(irb_params)
    if @irb.save
      #RegistrationMailer.signup_confirmation(@registration).deliver
      #log_in @user
      flash[:success] = "Your details have been registered. A confirmation email has been sent."
      redirect_to root_url
    else
      render 'new'
    end
  end

  def index
  end

  def edit
  end

  def show
  end

  private
  def irb_params
      params.require(:irb).permit(:proposalno, :apptype, :titleofproject, :acc1, :date1, :acc2, :date2, :acc3, :date3, :projtype, :fund, :fundingdetails, :datetobegindc, :statusofprininv, :typeofreview, :rationale, :abstract, :noofsub, :assowithsub, :natureofassociation, :subjectselection, :ressubcomp, :adforresparti, :confidentiality, :voluntaryparticipation, :howwhereconsent, :methodproceduresubjectparti, :childrenpermission, :infowithheld, :risk, :riskbenefitratio, :minimizingrisk, :otherapptype, status:[])
  end
end

这是迁移:

class CreateIrbs < ActiveRecord::Migration[5.0]
  def change
    create_table :irbs do |t|
      t.string :proposalno
      t.text :status
      t.string :responsibility
      t.string :apptype
      t.string :titleofproject
      t.string :acc1
      t.string :date1
      t.string :acc2
      t.string :date2
      t.string :acc3
      t.string :date3
      t.string :projtype
      t.string :fund
      t.string :fundingdetails
      t.string :datetobegindc
      t.string :statusofprininv
      t.string :typeofreview
      t.string :rationale
      t.string :abstract
      t.string :poputype
      t.string :noofsub
      t.string :assowithsub
      t.string :natureofassociation
      t.string :subjectselection
      t.string :ressubcomp
      t.string :adforresparti
      t.string :confidentiality
      t.string :voluntaryparticipation
      t.string :howwhereconsent
      t.string :methodproceduresubjectparti
      t.string :childrenpermission
      t.string :infowithheld
      t.string :risk
      t.string :riskbenefitratio
      t.string :minimizingrisk
      t.string :otherapptype
      t.timestamps
    end
  end
end

型号代码:

class Irb < ApplicationRecord
  serialize :status
end

最佳答案

您在这里插入的是散列而不是数组。简而言之,哈希值是由键和值的集合组成的。

正如 okomikeruko 所提到的,最好定义具有文本数据类型的列。尽管我认为这里的长度不会超过 255 个字符,至少在您问题中提供的示例中是这样。

我相信问题出在您的 permit 方法内部。对于名为 :status: 的键,您定义了 0 个允许在哈希中使用的键。这就是问题的根源。

您所需要的只是是否指定键 ..., status: [:group_one, ...] 如果您知道它们,或者它们是动态的,可以与您认为的另一个解决方案一起使用可以在下面的链接中找到。

https://github.com/rails/rails/issues/9454

Rails4: How to permit a hash with dynamic keys in params?

关于mysql - 哈希未插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40117190/

相关文章:

ruby - 如何使用 Ruby 在 Appium 中验证键盘是否打开

ruby - 如何使用 rake 运行指定目录中的所有 ruby​​ 文件

mysql - GROUP 列,其中其他列条件为 AND

php - 如何一次将大量数据插入mySQL数据库?

ruby-on-rails - 如何将 Git 子目录推送到 heroku?

ruby-on-rails - Heroku 部署的 Bootstrap 和 Rails 问题

php - 从数据库中抓取数据并转化为变量

mysql - Django 在将数据保存到模型中时显示错误 : django. db.utils.IntegrityError : (1048, "Column ' id' 不能为 null")

ruby-on-rails - 在 capybara /rspec规范期间未加载 Assets

ruby - 防止 ruby​​ 出现 "No buffer space available"错误