在此过程中可能会出现一些问题。我也有使用引导简单形式的问题。在我修复了控制器、模型和表单中的所有内容后,它就可以工作了。
对我来说,我遇到了几个问题,尤其是对我来说至关重要的注释行。
检查您是否具备以下条件:
survey.rb:
class Survey < ApplicationRecord
has_many :answers
accepts_nested_attributes_for :answers, allow_destroy: true
#errors have to come from answer validation for answer form
validates_associated :answers
validates :question, :answers, presence: true
answer.rb:
class Answer < ApplicationRecord
belongs_to :survey
# make sure there is a validation on answer
validates :answer, presence: true
end
_form.html.slim
# make sure you have given the right attributes for .input and simple_fields_for
= f.simple_fields_for :answers, @survey.answers do |answer_form|
= answer_form.input :answer
surveys_controller.rb
def new
@survey = Survey.new
#when answers are not builded it wont show any simple fields for
@survey.answers.build
end
def create
@survey = Survey.new(survey_params)
@survey.user = current_backend_user
if @survey.save
redirect_to backend_surveys_path, notice: 'Umfrage erfolgreich erstellt'
else
render :new
end
end
def survey_params
# make sure everything is permitted correctly
params.require(:survey).permit(:some_attribute, ..., answers_attributes: %i[id answer])
end
reject if on model validation 有时也会导致错误。小心点。