I need to save items separately coming from a form of a text field, but my code is saving these items duplicate form.
My 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
My model
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
Thanks!
Aucun commentaire:
Enregistrer un commentaire