Ruby on rails 如何在一个rails表单中处理多个模型?
我有以下型号Ruby on rails 如何在一个rails表单中处理多个模型?,ruby-on-rails,ruby,nested-forms,Ruby On Rails,Ruby,Nested Forms,我有以下型号 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 :ques
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
如何在3个以上的模型中保存数据?
目前,我可以将调查表格中的数据保存到调查、调查部分和问题模型中。但是我不知道我必须在控制器中做些什么才能将数据保存到其他模型中。如果您正确使用助手,您可以根据需要处理任意多个表单 我想这就是你做不到的地方(你的控制器看起来不错) 我也回过一段时间
#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
#app/models/survey.rb
班级调查
尽量使您的模型名称尽可能简洁
#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 %>
#app/controllers/surveys_controller.rb
类SurveysController<应用程序控制器
def新
@survey=survey.new
@调查.部分.构建.问题.构建
结束
def创建
@测量=测量。新测量参数
@调查。保存
结束
私有的
def测量参数
参数要求(:调查)。允许(:标题,章节属性:[:标题,问题属性:[:标题]]
结束
结束
#app/views/surveys/new.html.erb
对于相同类型的车型,您可以在此处获得最佳解释
现在看来
<%= form_for @survey do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :sections do |builder| %>
<%= builder.text_field :title %>
<%= builder.fields_for :questions do |question| %>
<%= question.text_field :content%>
<% end %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
我的建议是避免使用嵌套表单。这就是所谓的rails方式,但它增加了很多复杂性和耦合性。最好手动创建表单,并使用表单对象来处理它。你可以用谷歌搜索这个方法。我花了半个小时和大约40个chrome标签找到了这个答案,在2分钟内解决了我的问题。非常感谢。
#app/models/survey.rb
class Survey < ActiveRecord::Base
has_many :sections, :dependent => :destroy
accepts_nested_attributes_for :sections, :allow_destroy => true
end
#app/models/section.rb
class Section < ActiveRecord::Base
belongs_to :survey
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :allow_destroy => true
end
#app/models/question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :answers
end
def new
@survey = Survey.new
section = @survey.sections.build
section.questions.build
end
end
<%= form_for @survey do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :sections do |builder| %>
<%= builder.text_field :title %>
<%= builder.fields_for :questions do |question| %>
<%= question.text_field :content%>
<% end %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>