Ruby on rails 参数缺失或值为空:事件数据
我是RubyonRails新手,在将包含数据的csv文件导入数据库时遇到了麻烦。我一直在四处寻找,但这里的大多数类似问题并没有解决这个问题 问题:参数缺失或值为空:事件数据 我知道此问题是由于params.require(但删除它没有帮助,因为csv文件只是临时上载,但没有从中提取数据)导致的操作控制器引起的 以下是支持文件=> 控制器:Ruby on rails 参数缺失或值为空:事件数据,ruby-on-rails,ruby,csv,Ruby On Rails,Ruby,Csv,我是RubyonRails新手,在将包含数据的csv文件导入数据库时遇到了麻烦。我一直在四处寻找,但这里的大多数类似问题并没有解决这个问题 问题:参数缺失或值为空:事件数据 我知道此问题是由于params.require(但删除它没有帮助,因为csv文件只是临时上载,但没有从中提取数据)导致的操作控制器引起的 以下是支持文件=> 控制器: class IncidentDataController < ApplicationController before_action :set_in
class IncidentDataController < ApplicationController
before_action :set_incident_datum, only: [:show, :edit, :update, :destroy]
# GET /incident_data
# GET /incident_data.json
def index
@incident_data = IncidentDatum.all
@incident_data_first_20 = IncidentDatum.first(20)
end
def import
IncidentDatum.import(params[:file])
if @IncidentDatum.save
# after import, redirect and let us know the method worked!
redirect_to root_url, notice: "Incident Data Imported!"
else
render :new
end
end
# GET /incident_data/1
# GET /incident_data/1.json
def show
@distinct_number = IncidentDatum.distinct.pluck(:number).sort
end
# GET /incident_data/new
def new
@incident_datum = IncidentDatum.new
end
# GET /incident_data/1/edit
def edit
end
# POST /incident_data
# POST /incident_data.json
def create
@incident_datum = IncidentDatum.new(incident_datum_params)
respond_to do |format|
if @incident_datum.save
format.html { redirect_to @incident_datum, notice: 'Incident datum was successfully created.' }
format.json { render :show, status: :created, location: @incident_datum }
else
format.html { render :new }
format.json { render json: @incident_datum.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /incident_data/1
# PATCH/PUT /incident_data/1.json
def update
respond_to do |format|
if @incident_datum.update(incident_datum_params)
format.html { redirect_to @incident_datum, notice: 'Incident datum was successfully updated.' }
format.json { render :show, status: :ok, location: @incident_datum }
else
format.html { render :edit }
format.json { render json: @incident_datum.errors, status: :unprocessable_entity }
end
end
end
# DELETE /incident_data/1
# DELETE /incident_data/1.json
def destroy
@incident_datum.destroy
respond_to do |format|
format.html { redirect_to incident_data_url, notice: 'Incident datum was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_incident_datum
@incident_datum = IncidentDatum.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def incident_datum_params
params.require(:incident_datum).permit(
:number,
:priority,
:state,
:description,
:short_description1,
:ticket_type,
:task_type,
:opened,
:closed,
:initial_contact,
:configuration_item,
:environment,
:assignment_group,
:reassignment_count,
:department,
:name,
:problem,
:short_description2)
#MG what to do with '.require(:incident_datum)' after params?
end
end
控制台:
Processing by IncidentDataController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lBXX210GVPazui46U8rATpzrXCMJw8DAqb7Hc3WWK2uTU3WGojClRU7+ZKkakCjUblweOV7UpM+FiIvRSNzSng==", "file"=>#<ActionDispatch::Http::UploadedFile:0x00000002829388 @tempfile=#<Tempfile:/tmp/RackMultipart20180110-11292-jzqi2l.csv>, @original_filename="incident_datum.csv", @content_type="application/vnd.ms-excel", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"incident_datum.csv\"\r\nContent-Type: application/vnd.ms-excel\r\n">, "commit"=>"Import CSV"}
Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: incident_datum):
app/controllers/incident_data_controller.rb:84:in `incident_datum_params'
app/controllers/incident_data_controller.rb:39:in `create'
^C- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2018-01-10 14:20:33 +0000 ===
- Goodbye!
Exiting
IncidentDataController处理#创建为HTML
参数:{“utf8”=>“✓", "真实性令牌“=>”lBXX210GVPazui46U8rATpzrXCMJw8DAqb7Hc3WWK2uTU3WGojClRU7+ZKkakCjUblweOV7UpM+FiIvRSNzSng==”,“文件”=>,“提交”=>“导入CSV”}
在2ms内完成400个错误请求(ActiveRecord:0.0ms)
ActionController::ParameterMissing(param缺失或值为空:事件数据):
app/controllers/incident_data_controller.rb:84:in“incident_datum_params”
app/controllers/incident\u data\u controller.rb:39:in'create'
^C-优雅地停止,等待请求完成
puma关闭时间:2018-01-10 14:20:33+0000===
-再见!
退出
谢谢!我认为你的问题在于你的表单(如果你想保持rails惯例),你必须创建一个IncidentDatum元素,并为这个元素创建一个表单,比如:
<%= form_for @incident_data , multipart: true do %>
...
您还需要将文件
添加到许可
params.require(:incident_datum).permit( :number , :file , ....
您可以在console浏览器的“网络”选项卡以及您在问题中发布的日志中查看帖子的格式:
IncidentDataController处理#创建为HTML参数:
{“utf8”=>“✓",
“真实性令牌”=>“LBXX210GVPAZUI46U8RATPZRXCMJW8DAQB7HC3WWK2UTU3WGOJCRU7+ZKkakCjUblweOV7UpM+FiIvRSNzSng=”,
“文件”=>#,
@原始\u filename=“incident\u datum.csv”,
@content_type=“application/vnd.ms excel”,
@headers=“内容处置:表单数据;名称=\”文件\”;
文件名=\“事件数据.csv\”\r\n内容类型:
application/vnd.ms excel\r\n“>,“提交”=>“导入CSV”}已完成400
2ms内的错误请求(ActiveRecord:0.0ms)
您可以在参数中直接看到
文件
(无内部事件数据事件数据:{fiel:..})您试图发布到哪个操作?创建
或导入
?在导入
操作中规避强参数有什么原因吗?您使用参数[:文件]
而不是事件数据参数[:file]
。我想,@inye发现了您的问题。您的表单字段(以及相应的参数)没有按照Rails约定命名。您的字段应该是'width:600px;'%>
,但正如inye上面指出的那样,它更干净,更易于使用Rails的表单生成器!
Processing by IncidentDataController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lBXX210GVPazui46U8rATpzrXCMJw8DAqb7Hc3WWK2uTU3WGojClRU7+ZKkakCjUblweOV7UpM+FiIvRSNzSng==", "file"=>#<ActionDispatch::Http::UploadedFile:0x00000002829388 @tempfile=#<Tempfile:/tmp/RackMultipart20180110-11292-jzqi2l.csv>, @original_filename="incident_datum.csv", @content_type="application/vnd.ms-excel", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"incident_datum.csv\"\r\nContent-Type: application/vnd.ms-excel\r\n">, "commit"=>"Import CSV"}
Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: incident_datum):
app/controllers/incident_data_controller.rb:84:in `incident_datum_params'
app/controllers/incident_data_controller.rb:39:in `create'
^C- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2018-01-10 14:20:33 +0000 ===
- Goodbye!
Exiting
<%= form_for @incident_data , multipart: true do %>
...
params.require(:incident_datum).permit( :number , ....
params.require(:incident_datum).permit( :number , :file , ....