无法将表格中的数据存储到Rails的哈希图中?

时间:2019-03-14 00:22:51

标签: ruby-on-rails ruby

我正在尝试将所有患者全名从数据库表存储到哈希图中,但是出现以下错误:

param is missing or the value is empty: patient 

在我的患者控制器的这一行:

def patient_params
      params.require(:patient).permit(:Full_Name, :Age, :Sex, :Address, :PPS, :Medical_Card)
end

我不确定这是哪里出了问题,任何帮助将不胜感激!

patients_controller.rb

  def allP
    # print out all
    p=Hash.new
    current_user.patients.all.each do |patient|
        p.store("Full Name", patient.Full_Name)
    end
  end

index.html.erb

<%= button_to "Some Button", :method=> "allP" %>

错误日志

Started POST "/patients?method=allP" for 127.0.0.1 at 2019-03-14 00:36:54 +0000
Processing by PatientsController#create as HTML
  Parameters: {"authenticity_token"=>"lo2SRaVmEAHUdpqgknqT9iKPijEypaJ+8PX5Ad1igd/VTBbnRWsZy7akIVBx9c3eQnEmh6lljGm1sfEIDNs4Eg==", "met

hod"=>"allP"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1
]]
  ↳ /Users/j/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Completed 400 Bad Request in 3ms (ActiveRecord: 0.3ms)



ActionController::ParameterMissing (param is missing or the value is empty: patient):

app/controllers/patients_controller.rb:83:in `patient_params'
app/controllers/patients_controller.rb:37:in `create'

1 个答案:

答案 0 :(得分:1)

检查这些参数(为便于阅读而设置格式):

Started POST "/patients?method=allP" for 127.0.0.1 at 2019-03-14 00:36:54 +0000
Processing by PatientsController#create as HTML
  Parameters: {
    "authenticity_token"=>"lo2SRaVmEAHUdpqgknqT9iKPijEypaJ+8PX5Ad1igd/VTBbnRWsZy7akIVBx9c3eQnEmh6lljGm1sfEIDNs4Eg==", 
    "method"=>"allP"
  }

所以,当您说:

def patient_params
  params.require(:patient).permit(:Full_Name, :Age, :Sex, :Address, :PPS, :Medical_Card)
end

params.require(:patient)之所以失败,是因为等待,params不包含:patient,而您require则成功了。