在Ruby On Rails中保存调查的答案

时间:2018-11-06 04:48:34

标签: ruby-on-rails

我有3个模型,调查,问题和答案,调查是嵌套Question的主要模型,在创建新表格时,我可以创建并完美编辑调查及其问题的名称,但是,我创建了一个视图和一种称为“获取”的方法,在该视图中,我让用户进行了调查,为此,我在“问题”中嵌套了“答案”,而问题在“调查”中嵌套了,答案在阅读模式下可见为了避免重新存储或更新,在保存该表单时,我只需要保存“答案”,即,需要创建一个新的Answer实例,并存储该答案,但是我尽管在窗体的嵌套中添加了“ Answer.new”,但仍然无法执行此操作,而且我可以看到您没有注意控制器,特别是在Update方法中,您可以通过哪种方式正确保留问题?

take.html.erb

<%= form_with(model: @survey, local: true) do |form| %>

  <% if @survey.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2>

      <ul>
      <% @survey.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.object.title %>
  </div>

  <div class="fields">
    <%= form.fields_for :questions do |question| %>
      <%= question.object.question_title %> 
      <ol type="a">
        <%= question.fields_for :answers, Answer.new do |answer| %>
          <li><%= answer.text_field :response, placeholder: "Reponse" %></li>
        <% end %>
      </ol>
    <% end %>
  </div>

  <div class="actions">
    <br> <%= form.submit %>
  </div>

<% end %>

surveys_controller.rb

class SurveysController < ApplicationController
  before_action :set_survey, only: [:show, :edit, :take, :update, :destroy]

  def index
    @surveys = Survey.all
  end

  def show
  end

  def take
  end

  def new
    @survey = Survey.new
    @question = @survey.questions.build
  end

  def edit
    @question = @survey.questions.build
  end

  def create
    @survey = Survey.new(survey_params)

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

  def update
    respond_to do |format|

      @answer = Answer.new(id: params[:id], response: params[:response], question_id: params[:question_id])

      if @survey.update(survey_params)
        format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
        format.json { render :show, status: :ok, location: @survey }
      else
        format.html { render :edit }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @survey.destroy
    respond_to do |format|
      format.html { redirect_to surveys_url, notice: 'Survey was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_survey
      @survey = Survey.find(params[:id])
    end

    def survey_params
      params.require(:survey).permit(:title, questions_attributes: [:id, :question_title, :survey_id])
    end
end

模型

class Survey < ApplicationRecord
    has_many :questions, dependent: :destroy
    accepts_nested_attributes_for :questions
end

class Question < ApplicationRecord
  belongs_to :survey
  has_many :answers
  accepts_nested_attributes_for :answers
end

class Answer < ApplicationRecord
  belongs_to :question
end

1 个答案:

答案 0 :(得分:0)

  

我只需要保存“答案”,即我需要一个新实例   要创建的答案,并存储该答案,但是我不能做   尽管在表格的嵌套中添加了“ Answer.new”,但我可以   看到您没有注意控制器,特别是   在Update方法中,您可以通过什么方式保留问题   正确吗?

好吧,您不保存update操作中的答案!简而言之,您缺少@answer.save

@answer = Answer.new(response: params[:response], question_id: params[:question_id])
@answer.save #you are missing this line

注意:

如您所见,我已经从id: params[:id]中删除了Answer.new,因为它可能会为重复的条目引发ActiveRecord::RecordNotUnique