gpt4 book ai didi

ruby-on-rails - 打破参数并单独保存项目

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:15 25 4
gpt4 key购买 nike

我需要单独保存来自文本字段表单的项目,但我的代码将这些项目保存为重复表单。

我的 Controller

def create

@answer_option = AnswerOption.break_options(answer_option_params)
@answer_option = AnswerOption.new(answer_option_params)

respond_to do |format|
if @answer_option.save
format.html { redirect_to @answer_option, notice: 'Answer option was successfully created.' }
format.json { render :show, status: :created, location: @answer_option }
else
format.html { render :new }
format.json { render json: @answer_option.errors, status: :unprocessable_entity }
end
end
end

我的模型

class AnswerOption < ActiveRecord::Base
belongs_to :question

def self.break_options(var)
ugly_answers = var[:content].split /[\r\n]+/
ugly_answers.each do |answer|
AnswerOption.create!(content: answer)
end
end

end

谢谢!

最佳答案

def create
@answer_option = AnswerOption.break_options(answer_option_params)
end

关于ruby-on-rails - 打破参数并单独保存项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31757394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com