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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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 在RoR中创建有意重复的记录_Ruby On Rails - Fatal编程技术网

Ruby on rails 在RoR中创建有意重复的记录

Ruby on rails 在RoR中创建有意重复的记录,ruby-on-rails,Ruby On Rails,我的目标是创建空白问题,然后分配给编辑来创建 为此,我尝试创建一个表单,在其中输入主题和问题,然后让后端在该表单上迭代,以创建x个故意重复的问题 控制器: def create @question = [] 5.times do @question = Question.new(question_params) end end 参数: Parameters: {"utf8"=>"✓", "authenticity_token"=>"unfqSHnfdN

我的目标是创建空白问题,然后分配给编辑来创建

为此,我尝试创建一个表单,在其中输入主题和问题,然后让后端在该表单上迭代,以创建x个故意重复的问题

控制器:

def create
   @question = []
   5.times do
    @question = Question.new(question_params)
   end
  end
参数:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"unfqSHnfdNhidUCvLf3Zck8MeP59Qobe2bJz0tUhWQ1SUh29a8LdoGAYpUwbOOJS8U+wzlDQVBXQYcKeRqLDmQ==", "question"=>{"name"=>"four", "topic_id"=>"1"}, "commit"=>"Save "}
上面的想法只创建了一条记录,我想这是因为控制器正在迭代,但只有一组来自表单的参数输入

我是不是完全错了

编辑 全工作控制器为子孙后代,thx的答案如下

controller do
def create
  if @question = 5.times.each_with_object([]) do |_, to_return|
    to_return << Question.create(question_params)
    end
    redirect_to admin_questions_path, notice: "Questions created"
  else
   # Handle failure
   redirect_to admin_questions_path, notice: "Questions NOT created"
  end
end
controller-do
def创建
如果@question=5.次,则每个带有_对象([])的| _都要返回|

要返回不如做一些更像:

def create
  5.times.each_with_object([]) do |_, to_return|
    to_return << Question.create(question_params)
  end
end
def创建
5.次。每个带有_对象([])的| u都要返回|

要返回,您需要
new
。哪里是
save
?您还将用每个循环覆盖
@question
数组。你想要的是类似于
@question的东西