Tag: 嵌套表格

如何在一个轨道中处理多个模型?

我有以下型号 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 = […]

嵌套表单字段出现问题

我正在尝试为我的网站实现嵌套对象表单,使用Ryan Daigle的博客文章作为指南( http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-嵌套属性 )。 由于某种原因,嵌套的表单字段不会出现在视图中。 class Instruction < ActiveRecord::Base has_many :steps accepts_nested_attributes_for :steps end class Step < ActiveRecord::Base belongs_to :instruction end 当我更改instruction_form.fields_for :steps do |step_form| to instruction_form.fields_for :step do |step_form| ,表单呈现,但提交后,我得到一个’未知属性:步’错误。 我正在做的似乎与教程相符。 我应该检查什么? 谢谢。

Rails 4嵌套属性不保存

我似乎无法获得嵌套属性来保存到数据库。 我正在使用Rails 4。 这是我的模特: class Answer < ActiveRecord::Base belongs_to :question end class Question < ActiveRecord::Base has_many :answers belongs_to :survey accepts_nested_attributes_for :answers, allow_destroy: true end class Survey < ActiveRecord::Base has_many :questions validates_presence_of :name accepts_nested_attributes_for :questions 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.’ […]

rails中的嵌套表单 – 访问has_many关系中的属性

我有一个用户和个人资料模型。 一个用户可以拥有许多配置文件 在用户创建过程中,我只需要在我的用户模型中访问配置文件部分中的一个信息(即电话号码)。 因此,我试图通过attr_accessible完成它。 我的user.rb看起来像这样。 has_many :profiles attr_accessible :handle, :email, :password, :profile_mobile_number attr_accessor : :profile_mobile_number 我面临的问题是,当我尝试在user.rb中的方法中调用getter方法profile_mobile_number时(该方法是私有的,虽然我认为无关紧要),但我得到一个空值。 我在users / new.html.erb表单中使用以下内容 我的问题是这样做的正确方法是什么? 我应该使用 或 (注意第二个是复数)。 当我使用复数:配置文件时,我甚至看不到表单上的字段。 我在这里想念的是什么? 什么是需要在模型user.rb中使用的时态? :profile_phone_number或:profiles_phone_number? 谢谢。