我想将.xlsx tempfile从表单传递给导入模型。但是,我遇到了ActiveModel::ForbiddenAttributesError
。我正确地定义了强参数,但是在处理没有属性的类的虚拟列时可能存在特殊性。
模型逻辑在指向本地文件路径时起作用,因此我将大部分内容留下。这是我的代码:
new.html.erb
<%= form_for @package_import do |f| %>
<%= f.file_field :file %>
<%= f.submit "Import" %>
<% end %>
package_imports_controller.rb
class PackageImportsController < ApplicationController
def new
@package_import = PackageImport.new
end
def create
@package_import = PackageImport.new(params[:package_import])
if @package_import.save
redirect_to root_url, notice: "Imported packages"
else
render :new
end
end
private
def package_import_params
params.require(:package_import).permit(:file)
end
end
package_import.rb
class PackageImport
include ActiveModel::Model
...
end
答案 0 :(得分:1)
您需要在params[:package_import]
方法的package_import_params
中将@package_import = PackageImport.new(params[:package_import])
更改为create
。
def create
@package_import = PackageImport.new(package_import_params)
if @package_import.save
redirect_to root_url, notice: "Imported packages"
else
render :new
end
end