如何以一种导轨形式处理多个模型? [英] How to handle multiple models in one rails form?

查看:81
本文介绍了如何以一种导轨形式处理多个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号

class Survey < ActiveRecord::Base
  has_many :survey_sections
  accepts_nested_attributes_for :survey_sections
end

class SurveySection < ActiveRecord::Base
  belongs_to :survey
  has_many :questions
  accepts_nested_attributes_for :questions
end

class Question < ActiveRecord::Base
  belongs_to :survey_section
  has_many :answers
  belongs_to :question_group
  accepts_nested_attributes_for :question_group
  accepts_nested_attributes_for :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
end

class QuestionGroup < ActiveRecord::Base
  has_many :questions
end

我的控制器:

 def new
    @survey = Survey.new
    survey_section = @survey.survey_sections.build
    survey_section.questions.build
  end

 def create
    @survey = Survey.new(survey_params)
    if @survey.save
      redirect_to @survey, notice: 'Super'
    else
      render 'new'
    end
  end

 def survey_params
      params.require(:survey).permit(:title, :description, survey_sections_attributes:[:id, :title, questions_attributes:[:id, :text, answers_attributes:[:id, :text]]])
    end

如何在3个以上的模型中保存数据? 目前,我可以将调查表中的数据保存到调查,调查部分和问题模型中.但是我不知道我必须在控制器中保存什么数据才能保存到其他模型中.

How it is possible to save data in more then 3 models? At the moment i can save from my survey form data into the survey, survey section and question model. But i don't know what i have to in the controller that i can save data into the other models.

推荐答案

如果使用

You can handle as many forms as you need, if you use the fields_for helper properly.

我认为这是您不足的地方(您的控制器似乎还可以).

This is where you're falling short I think (your controller seems okay).

我还早些时候写一个关于这个的答案.

#app/models/survey.rb
class Survey < ActiveRecord::Base
    has_many :sections
    accepts_nested_attributes_for :sections
end

#app/models/section.rb
class Section < ActiveRecord::Base
    belongs_to :survey
    has_many :questions
    accepts_nested_attributes_for :questions
end

#app/models/question.rb
class Question < ActiveRecord::Base
    belongs_to :section
    has_many :answers
end

尝试使模型名称尽可能简洁.

Try and keep your model names as succinct as possible.

#app/controllers/surveys_controller.rb
class SurveysController < ApplicationController
   def new
      @survey = Survey.new
      @survey.sections.build.questions.build
   end

   def create
      @survey = Survey.new survey_params
      @survey.save
   end

   private 

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

#app/views/surveys/new.html.erb
<%= form_for @survey do |f| %>
   <%= f.text_field :title %>
   <%= f.fields_for :sections do |section| %>
       <%= section.text_field :title %>
       <%= section.fields_for :questions do |question| %>
           <%= question.text_field :title %>
       <% end %>
   <% end %> 
   <%= f.submit %>
<% end %>

这篇关于如何以一种导轨形式处理多个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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