Excel导入:缺少参数或值为空
Excel Import: Param is missing or the value is empty
我不知道为什么,但有一天我的 excel 导入不再起作用了。
查看:
<h1>Import Data</h1>
<br>
<div class="col-md-12">
<center>
<h2>Import List</h2>
<br>
<%= form_tag import_marcels_path, multipart: true do %>
<%= file_field_tag :file %>
<br>
<%= submit_tag "Import" %>
<% end %>
</center>
</div>
控制器:
def import
Marcel.import(params[:file])
redirect_to root_url, notice: 'File imported.'
end
def create
@marcel = Marcel.new(marcel_params)
respond_to do |format|
if @marcel.save
format.html { redirect_to @marcel, notice: 'Marcel was successfully created.' }
format.json { render :show, status: :created, location: @marcel }
else
format.html { render :new }
format.json { render json: @marcel.errors, status: :unprocessable_entity }
end
end
end
def marcel_params
params.require(:marcel).permit(:first_name, :last_name, :ldap)
end
型号:
class Marcel < ActiveRecord::Base
def self.import(file)
spreadsheet = Roo::Spreadsheet.open(file.path)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
marcel = find_by(id: row["id"]) || new
marcel.attributes = row.to_hash
marcel.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::CSV.new(file.path, nil, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
end
routes.rb:
# resources :marcels do
# collection do
# post :import
# end
# end
get 'marcels' => 'marcels#index'
post 'import_marcels' => 'marcels#import'
现在我收到以下错误:
param is missing or the value is empty: marcel
# Never trust parameters from the scary internet, only allow the white list through.
def marcel_params
params.require(:marcel).permit(:first_name, :last_name, :ldap)
end end
{"utf8"=>"✓",
"authenticity_token" => "tR2XHWpu1RB58fGMlAQQKXcQDD8ka8alVpTFztd7G5crgF0UfYwZHUNUN6+NsRF3AocT+2ljEZFNE2i+19Sfbw==",
"file"=>#<ActionDispatch::Http::UploadedFile:0x007f2cac0c2cb0
@tempfile=#<Tempfile:/tmp/RackMultipart20180322-6630-1fsopb3.csv>,
@original_filename="test.csv",
@content_type="application/vnd.ms-excel",
@headers="Content-Disposition: form-data; name=\"file\";
filename=\"test.csv\"\r\nContent-Type: application/vnd.ms-excel\r\n">,
"commit"=>"Import"}
我不知道为什么现在会发生这种情况,但也许你们中有人知道我该如何解决这个问题。我从 def marcel_params 中排除了 .require(:marcel)。导入后没有错误发生,但 Excel 的值未保存在数据库中。
感谢 TarynEast,我找到了解决该问题的方法。路线有些东西没有像预期的那样工作所以我改变了路线:
resources :marcels do
collection do
post :import
end
end
对此:
get 'marcels' => 'marcels#index'
post 'import_marcels' => 'marcels#import'
在此之后,导入工作正常。
我不知道为什么,但有一天我的 excel 导入不再起作用了。
查看:
<h1>Import Data</h1>
<br>
<div class="col-md-12">
<center>
<h2>Import List</h2>
<br>
<%= form_tag import_marcels_path, multipart: true do %>
<%= file_field_tag :file %>
<br>
<%= submit_tag "Import" %>
<% end %>
</center>
</div>
控制器:
def import
Marcel.import(params[:file])
redirect_to root_url, notice: 'File imported.'
end
def create
@marcel = Marcel.new(marcel_params)
respond_to do |format|
if @marcel.save
format.html { redirect_to @marcel, notice: 'Marcel was successfully created.' }
format.json { render :show, status: :created, location: @marcel }
else
format.html { render :new }
format.json { render json: @marcel.errors, status: :unprocessable_entity }
end
end
end
def marcel_params
params.require(:marcel).permit(:first_name, :last_name, :ldap)
end
型号:
class Marcel < ActiveRecord::Base
def self.import(file)
spreadsheet = Roo::Spreadsheet.open(file.path)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
marcel = find_by(id: row["id"]) || new
marcel.attributes = row.to_hash
marcel.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::CSV.new(file.path, nil, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
end
routes.rb:
# resources :marcels do
# collection do
# post :import
# end
# end
get 'marcels' => 'marcels#index'
post 'import_marcels' => 'marcels#import'
现在我收到以下错误:
param is missing or the value is empty: marcel
# Never trust parameters from the scary internet, only allow the white list through.
def marcel_params
params.require(:marcel).permit(:first_name, :last_name, :ldap)
end end
{"utf8"=>"✓",
"authenticity_token" => "tR2XHWpu1RB58fGMlAQQKXcQDD8ka8alVpTFztd7G5crgF0UfYwZHUNUN6+NsRF3AocT+2ljEZFNE2i+19Sfbw==",
"file"=>#<ActionDispatch::Http::UploadedFile:0x007f2cac0c2cb0
@tempfile=#<Tempfile:/tmp/RackMultipart20180322-6630-1fsopb3.csv>,
@original_filename="test.csv",
@content_type="application/vnd.ms-excel",
@headers="Content-Disposition: form-data; name=\"file\";
filename=\"test.csv\"\r\nContent-Type: application/vnd.ms-excel\r\n">,
"commit"=>"Import"}
我不知道为什么现在会发生这种情况,但也许你们中有人知道我该如何解决这个问题。我从 def marcel_params 中排除了 .require(:marcel)。导入后没有错误发生,但 Excel 的值未保存在数据库中。
感谢 TarynEast,我找到了解决该问题的方法。路线有些东西没有像预期的那样工作所以我改变了路线:
resources :marcels do
collection do
post :import
end
end
对此:
get 'marcels' => 'marcels#index'
post 'import_marcels' => 'marcels#import'
在此之后,导入工作正常。