打破参数并单独保存项目

时间:2015-08-01 01:43:33

标签: ruby-on-rails ruby ruby-on-rails-4

我需要单独保存来自文本字段形式的项目,但我的代码是将这些项目保存为副本。

我的控制器

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

谢谢!

1 个答案:

答案 0 :(得分:1)

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