我正试图导入.csv来填充和完成许多表单,而不是一个接一个地填充和创建它们。我在这里遵循了一个非常漂亮的教程:https://www.youtube.com/watch?v=W8pohTautj8这真的很有帮助。然后,我在这里为嵌套资源进行了调整:https://gorails.com/forum/import-a-csv-with-associations
到目前为止还不错。然后,我做了一些最后的调整,以解决Authentity令牌、NilClass错误的问题,现在我得到了错误数量的参数。我看到的所有同样出现这个错误的堆栈溢出帖子都无法提供帮助。
这是代码:
控制器
def create
@development = Development.find(params[:development_id])
@lot = @development.lots.create(params.require[:lot].permit(:status, :lot_number ,:stage, :land_size, :price, :bed, :bath, :car, :rent))
respond_to do |format|
if @lot.save
format.js
format.html { redirect_to Development.find(params[:development_id]), notice: "Lot was successfully created." }
format.json { render :show, status: :created, location: @lot }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @lot.errors, status: :unprocessable_entity }
end
end
end
def import
@development = Development.find(params[:development_id])
@lot = @development.lots.create(params.require[:lot].permit(:status, :lot_number ,:stage, :land_size, :price, :bed, :bath, :car, :rent))
Lot.import(@development, development_params[:lot])
redirect_to development_path, notice: "Lots were successfully created."
end
型号
def self.import(development, file)
CSV.foreach(file.path, headers: true) do |row|
development.lots.create! row.to_hash
end
end
_form partial
<%= form_tag import_development_lots_path, multipart: true, remote: true, authenticity_token: true do %>
<%= file_field_tag :file %>
<%= hidden_field_tag :authenticity_token, form_authenticity_token -%>
<%= submit_tag "Upload csv" %>
<% end %>
注意:这是对创建对象的常规原始表单(两者都在同一_form partial上(的补充。据我所知,原始数据表可以作为手动创建数据表的一种方式,一次一个,作为批量csv导入选项的另一个选项。如果我也需要添加原始表单的代码,请告诉我。他们似乎可以在同一页上共存。
路线
resources :lots do
collection { post :import }
end
应用程序.rb
require 'csv'
我相信这就是与csv导入相关的一切;非常感谢您的帮助!
编辑:这是Siim请求的错误日志。
Started POST "/developments/2/lots" for ::1 at 2021-05-13 10:46:14 +1000
Processing by LotsController#create as HTML
Parameters: {"authenticity_token"=>"s7EpVu4LymhZpm+nSxGF+yLXWY3GqHjwKSN0eccRCws7eTy1riGg9GplIciLIns7hlKYyRipA5tgMbGn0GTGow==", "file"=>"Test Lot.csv", "commit"=>"Upload Stocklist", "development_id"=>"2"}
Development Load (1.3ms) SELECT "developments".* FROM "developments" WHERE "developments"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/controllers/lots_controller.rb:71:in `get_development'
CACHE Development Load (0.0ms) SELECT "developments".* FROM "developments" WHERE "developments"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/controllers/lots_controller.rb:25:in `create'
Completed 500 Internal Server Error in 14ms (ActiveRecord: 1.3ms | Allocations: 1798)
ArgumentError (wrong number of arguments (given 0, expected 1)):
app/controllers/lots_controller.rb:26:in `create'
根据您的错误回溯(来自注释(,错误在以下行:
@lot = @development.lots.create(params.require[:lot].permit(:status, :lot_number ,:stage, :land_size, :price, :bed, :bath, :car, :rent))
如果你把它分解成多行,你会得到一个更准确的错误信息来自己找出它,但我已经看到了。
require
是一个需要参数的方法。您在不带参数的情况下调用它,然后通过对其调用[]
将其作为数组/哈希进行访问。
使用params.require(:lot)
而不是params.require[:lot]
。