我正在尝试将数组插入数据库。我可以在参数中看到数组 (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, ...]
如果您知道它们,或者它们是动态的,可以与您认为的另一个解决方案一起使用可以在下面的链接中找到。
关于mysql - 哈希未插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40117190/