Rails 4-无法保存嵌套模型(2个级别) [英] Rails 4 - Nested models(2 levels) not saving

查看:65
本文介绍了Rails 4-无法保存嵌套模型(2个级别)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于嵌套模型的Noob问题.

Noob question on nested models.

我正在使用Rails 4并尝试创建如下的嵌套模型:

I am using Rails 4 and trying to create nested models as below:

调查有很多问题 每个问题都有很多答案

Survey has many questions Each Question has many answers

我正在跟踪 Rails Casts第196集来创建调查,问题和答案以相同的形式出现. Surevey和Realted问题会保存,但答案不会保存到数据库.(但是,答案字段显示正确.)

I am following Rails Casts episode #196 to create a survey, questions and answers in the same form. Surevey and Realted questions get saved but answers don't get saved to the database.(The answers fields however are being displayed right.)

非常感谢您对此的投入.

I really appreciate your inputs on this.

谢谢, 迈克

surveys_controller.rb

def index
   @surveys = Survey.all
end

def new
  @survey = Survey.new
  3.times do
    question = @survey.questions.build
    1.times { question.answers.build }
  end
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 action: 'show', status: :created, location: @survey }
    else
      format.html { render action: 'new' }
      format.json { render json: @survey.errors, status: :unprocessable_entity }
    end
  end
end

def survey_params
  params.require(:survey).permit(:name,questions_attributes:[:content,answer_attributes:[:content]])
end

new.html.erb

<h1>New survey</h1>
  <%= render 'form' %>
<%= link_to 'Back', surveys_path %>

_form.html.erb:

<%= form_for(@survey) do |f| %>
   <% 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 |msg| %>
          <li><%= msg %></li>
          <% end %>
          </ul>
          </div>
   <%end%>

   <div class="field">
     <%= f.label :name %><br>
     <%= f.text_field :name %>
   </div>

   <!--Display Questions -->
   <%= f.fields_for :questions do |builder| %>
     <%= render 'question_fields', :f => builder%>
   <% end %>

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

<% end %>

_questions_fields.html.erb:

<p>
 <%= f.label :content, "Question" %><br />
 <%= f.text_area :content, :rows => 3 %>
</p>

<!--Display Answers -->
<%=f.fields_for :answers do |builder| %>
   <p>
     <%= render 'answer_fields', :f => builder%>
   </p>
<%end%>

_answers_fields.html.erb:

<p>
 <%= f.label :content, "Answer" %>
 <%= f.text_field :content%>
</p>

调查模型:

 class Survey < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions
 end

问题模型:

  class Question < ActiveRecord::Base
   belongs_to :survey
   has_many :answers, :dependent => :destroy
   accepts_nested_attributes_for :answers
  end

答案模型:

  class Answer < ActiveRecord::Base
  belongs_to :question
  end

推荐答案

尝试在survey_params方法中将answer_attributes更改为answers_attributes.

Try changing answer_attributes to answers_attributes in the survey_params method.

窥探会向您显示答案属性没有通过.

Prying would have shown you that the answer attributes didn't get through.

快速浏览后,我没有发现任何特别错误的内容,因此您只需要进行调试即可.您应该将gem 'pry-rails'添加到您的Gemfile中,然后放入

After a quick look through I don't see anything particularly wrong, so you just need to debug. You should add gem 'pry-rails' to your Gemfile, then put

require 'pry'; binding.pry

要调试的行上的

.我将其保存在调查控制器中的create动作中之前.然后提交表单并转到您的服务器.它会被暂停,并且您将有一个控制台.输入

on the line where you want to debug. I'd put it right before the save in your create action in the survey controller. Then submit the form and go to your server. It will be paused, and you will have a console. Type

@survey.save
@survey.errors

,看看会发生什么.还要键入paramssurvey_params以确保您的表单数据全部正确处理.

and see what comes up. Also type params and survey_params to make sure that your form data is all going through properly.

如果您继续在其他地方进行撬动,您会发现什么地方不起作用.我怀疑您的答案没有正确地放入Survey_params中.当事物得不到保存时,强大的属性通常是元凶.

If you keep prying in different places you'll figure out what's not working. I suspect that your answers aren't being pulled into survey_params properly. Strong attributes is often the culprit when things aren't getting saved.

这篇关于Rails 4-无法保存嵌套模型(2个级别)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆