未经许可的参数&无法显示嵌套的fields_for

时间:2018-05-03 19:50:46

标签: ruby-on-rails parameters ruby-on-rails-5 fields-for

我正在构建简单的ROR应用程序,其中包含调查问题和答案。使用脚手架方法生成调查,而问题和答案仅为模型。

Survey.rb

class Survey < ApplicationRecord
 has_many :questions
 accepts_nested_attributes_for :questions, allow_destroy: true
 validates :name, presence: true
end

Question.rb

class Question < ApplicationRecord
 belongs_to :survey
 has_many :answers
 accepts_nested_attributes_for :answers
 validates :question_content, presence: true
end

Answer.rb

class Answer < ApplicationRecord
 belongs_to :question
end

survey_controller.rb

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

def index
 @surveys = Survey.all
end

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

def new
 @survey = Survey.new
 @questions = @survey.questions.new
 @answers = @questions.answers.new
end

def edit
end

def create
 @survey = Survey.new(survey_params)
 Survey.create(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|
  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(:name, questions_attributes: [:id, :question_content], answers_attributes: [:id, :answer_content, :answer_type])
end
end

调查表单部分

<%= form_for(@survey) 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.label :name %>
<%= form.text_field :name, id: :survey_name %>
</div>

<%= form.fields_for :questions do |builder| %>
<fieldset>
  <%= builder.label :question_content, "Question" %><br/>
  <%= builder.text_area :question_content %><br/>
  <%= form.fields_for :answers do |f| %>
    <fieldset>
        <%= f.text_area :answer_type %>
        <%= f.text_area :answer_content %><br/>
    </fieldset>
</fieldset>
<% end %>
<%end %>


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

我想在单页中实施问答,即调查。任何帮助将不胜感激。

问题:无法将answers_attributes保存到数据库,但问题属性完全正常。我可能在控制器的某个地方犯了错误。

1 个答案:

答案 0 :(得分:0)

您需要更改构建答案字段的方式,以便将它们引用为fields_for large_table(nullable_foreign_key_with_index, tenant_id) ,而不是questions。尝试更改这段代码:

surveys

到此:

<%= form.fields_for :questions do |builder| %>
<fieldset>
  <%= builder.label :question_content, "Question" %><br/>
  <%= builder.text_area :question_content %><br/>
  <%= form.fields_for :answers do |f| %>
    <fieldset>
        <%= f.text_area :answer_type %>
        <%= f.text_area :answer_content %><br/>
    </fieldset>
</fieldset>
<% end %>
<%end %>

(注意:有意义的更改正在将<%= form.fields_for :questions do |builder| %> <fieldset> <%= builder.label :question_content, "Question" %><br/> <%= builder.text_area :question_content %><br/> <%= builder.fields_for :answers do |f| %> <fieldset> <%= f.text_area :answer_type %> <%= f.text_area :answer_content %><br/> </fieldset> <% end %> </fieldset> <% end %> 更改为form.fields_for