哈希不插入数据库

时间:2016-10-18 20:08:27

标签: mysql ruby-on-rails ruby

我正在尝试将数组插入数据库。我可以看到数组(状态" => {" group_one" =>"一个"," group_two" =>"一个& #34;})在参数中但是在插入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]]

这是我的控制器:

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

2 个答案:

答案 0 :(得分:0)

也许您应该尝试将数据类型从string更改为text

Using Rails serialize to save hash to database

答案 1 :(得分:0)

您正在此处插入Hash not Array。短Hash的差异由键和值的集合组成。

正如okomikeruko所提到的,最好定义一个带有text数据类型的列。虽然我不认为它超过了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?