Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 尝试导入.csv时,rails中的参数数量错误(给定0,应为1)_Ruby On Rails_Csv - Fatal编程技术网

Ruby on rails 尝试导入.csv时,rails中的参数数量错误(给定0,应为1)

Ruby on rails 尝试导入.csv时,rails中的参数数量错误(给定0,应为1),ruby-on-rails,csv,Ruby On Rails,Csv,我正在尝试导入一个.csv来填充和完成许多表单,而不是一个一个地填充和创建它们。我遵循了一个相当漂亮的教程:这真的很有帮助。然后,我在这里的帖子中对其进行了调整,以获得嵌套资源: 到目前为止还不错。然后,我做了一些最后的调整,以了解我在真实性令牌、类错误方面遇到的问题,现在我得到了错误数量的参数。我看到的所有出现此错误的堆栈溢出帖子都无法提供帮助 代码如下: 控制器 def create @development = Development.find(params[:developmen

我正在尝试导入一个.csv来填充和完成许多表单,而不是一个一个地填充和创建它们。我遵循了一个相当漂亮的教程:这真的很有帮助。然后,我在这里的帖子中对其进行了调整,以获得嵌套资源:

到目前为止还不错。然后,我做了一些最后的调整,以了解我在真实性令牌、类错误方面遇到的问题,现在我得到了错误数量的参数。我看到的所有出现此错误的堆栈溢出帖子都无法提供帮助

代码如下:

控制器

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_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 %>
\u形式部分

  def self.import(development, file)
    CSV.foreach(file.path, headers: true) do |row|
        development.lots.create! row.to_hash
    end
  end
<%= 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 %>
应用程序.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]

添加错误及其回溯,以便我们可以看到错误的来源以及接收错误参数的方法。@SiimLiiser已使用错误日志更新。如果你还需要看什么,请告诉我!错误是说错误在控制器的第26行。但是你只粘贴了一个片段,所以我不知道那是哪一行。啊,对了,这里应该是这一行:@lot=@development.lots.create(params.require[:lot]。permit(:status,:lot_number,:stage,:land_size,:price,:bed,:bath,:car,:rent))它是嵌套的,工作正常,但是我最近把它从params改为params。requireI最初把它改为include.require,因为我在'permit'Hi Siim中得到了一个NilClass错误,谢谢你!事实证明,这个问题只是更大问题中的一小部分。通过阅读本文,我能够理清整个问题:并重新构建我的csv上传,使其来自索引页面,而不是手动输入数据的现有表单。