ArgumentError(创建多个记录时参数数量错误

时间:2016-01-12 14:39:14

标签: ruby-on-rails

我有一个名为actors的数组,其中包含对象,每个对象都有关于一个演员的数据,例如他的名字等。

我试图通过Rails将数据保存在我的SQLite数据库中,但是我遇到了一些问题。

我在actor控制器中尝试了几个创建函数,

def create
  actors = Actor.create(params[:actors])
  redirect_to :root
end

这会创建一条新记录,但内容为emtpy,

{"id":8,"name":null,"character":null,"movie_id":null}

我想也许我需要在create函数中添加参数,如此,

def create
  actors = Actor.create(params[:actors], movie_id: params[:movie_id], name: params[:name], character: params[:character])
  redirect_to :root
end

但是这会返回错误

  

ArgumentError(参数数量错误(0..1为2))

*更新*

这是来自帖子请求的rails日志,

Started POST "/actors.json" for 127.0.0.1 at 2016-01-12 17:08:05 +0100
Processing by ActorsController#create as JSON
  Parameters: {"_json"=>[{"name"=>"Michael B. Jordan", "character"=>"Adonis Creed", "movie_id"=>312221}, {"name"=>"Sylvester Stallone", "character"=>"Rocky Balboa", "movie_id"=>312221}, {"name"=>"Graham McTavish", "character"=>"Tommy Holiday", "movie_id"=>312221}, {"name"=>"Tessa Thompson", "character"=>"Bianca", "movie_id"=>312221}, {"name"=>"Phylicia Rashād", "character"=>"Mary Anne Creed", "movie_id"=>312221}, {"name"=>"Hans Marrero", "character"=>"Flores", "movie_id"=>312221}, {"name"=>"Will Blagrove", "character"=>"James", "movie_id"=>312221}, {"name"=>"Tony Bellew", "character"=>"'Pretty' Ricky Conlan", "movie_id"=>312221}, {"name"=>"Ritchie Coster", "character"=>"Pete Sporino", "movie_id"=>312221}, {"name"=>"Jacob 'Stitch' Duran", "character"=>"Stitch", "movie_id"=>312221}, {"name"=>"Malik Bazille", "character"=>"Amir", "movie_id"=>312221}, {"name"=>"Wood Harris", "character"=>"Tony 'Little Duke' Burton", "movie_id"=>312221}, {"name"=>"Gabe Rosado", "character"=>"Leo 'The Lion' Sporino", "movie_id"=>312221}], "actor"=>{}}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.2ms)

2 个答案:

答案 0 :(得分:3)

您需要迭代params [:actors]哈希中的每个actor,并将其中每个参数的值分配给您的Actor模型。

def create
  actors = params[:actors].each do |key, value|
      Actor.create({
        movie_id: value[:movie_id], 
        name: value[:name],
        character: value[:character]
      })
  end
  redirect_to :root
end

免责声明:我怀疑这段代码是100%正确的,我不确定我是否正确地使用了语法来获取参数的值(这有助于查看参数是什么),但这应该会让你进入正确的方向。还可以尝试查看这些帖子:

Accessing array of parameters inside the params[] value

Build array of objects from params hash after Transaction block fails, Rails

编辑:试试这个......

def create
  actors = params[:json].each do |actor|
      Actor.create({
        movie_id: actor["movie_id"], 
        name: actor["name"],
        character: actor["character"]
      })
  end
  redirect_to :root
end

答案 1 :(得分:1)

您需要将哈希值传递给create方法。

执行以下操作:

  • 迭代params[:actors]

    中的每个对象
    params[:actors].each do |actor|
    
  • 通过从对象中提取值,使用值namecharactermovie_id创建以下哈希值。

    hsh = {
      :name => actor.name,   #Assuming your actor object has this property
      :character => actor.character,  #Similarly actor.character
      :movie_id => actor.movie_id     #actor.movie_id
    }
    
  • 现在您可以将其传递给create,如:

    Actor.create(hsh)
    

修改
看到您的错误日志后,您似乎需要遍历params[:_json]数组。

所以你可以这样做:

params[:_json].each do |actor|
  Actor.create(actor)
end

这应该有效,因为params[:_json]中的每个json文档都是一个actor哈希。