如何在1个表格中包含2个模型?

一个模型创建:name,:metric(Quantified.rb),另一个创建:result_value,:result_date(Result.rb)。

class Quantified  { where(categories: 'Monthly Average') } scope :instance, -> { where(categories: 'One-Time Instance') } has_many :results CATEGORIES = ['Monthly Average', 'One-Time Instance'] end class Result < ActiveRecord::Base belongs_to :user belongs_to :quantified end 

我需要在_form和/或控制器中做什么才能允许用户将他的results_date和results_value(结果_form)添加到相应的名称和指标(quantieds _form)?

   

prohibited this quantified from being saved:



[:month, :year], class: 'date-select' %>

prohibited this result from being saved:

[:month, :year], class: 'date-select' %>

例如用户输入:

:name = Ran

:metric =英里

然后从今以后的每个月他都会输入他那个月的平均里程数。 如何在这两种forms之间建立关系?

:result_value = 2.1

:result_date = 1月

:result_value = 1.8

:result_date = 12月

*每个名称/指标都有许多结果值/日期。

控制器

 class QuantifiedsController < ApplicationController before_action :set_quantified, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, except: [:index, :show] def index @averaged_quantifieds = current_user.quantifieds.averaged @instance_quantifieds = current_user.quantifieds.instance @results = Result.all.order("result_date") end def show end def new @quantified = current_user.quantifieds.build end def edit end def create @quantified = current_user.quantifieds.build(quantified_params) if @quantified.save redirect_to quantifieds_url, notice: 'Goal was successfully created' else render action: 'new' end end def update if @quantified.update(quantified_params) redirect_to quantifieds_url, notice: 'Goal was successfully updated' else render action: 'edit' end end def destroy @quantified.destroy redirect_to quantifieds_url end private def set_quantified @quantified = Quantified.find(params[:id]) end def correct_user @quantified = current_user.quantifieds.find_by(id: params[:id]) redirect_to quantifieds_path, notice: "Not authorized to edit this goal" if @quantified.nil? end def quantified_params params.require(:quantified).permit(:categories, :name, :metric, :result, :date) end end 

 class ResultsController < ApplicationController before_action :set_result, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, except: [:index, :show] def index @results = Result.all.order("result_date") end def show end def new @result = current_user.results.build end def edit end def create @result = current_user.results.build(result_params) if @result.save redirect_to @result, notice: 'Goal was successfully created' else render action: 'new' end end def update if @result.update(result_params) redirect_to @result, notice: 'Goal was successfully updated' else render action: 'edit' end end def destroy @result.destroy redirect_to results_url end private def set_result @result = Result.find(params[:id]) end def correct_user @result = current_user.results.find_by(id: params[:id]) redirect_to results_path, notice: "Not authorized to edit this result" if @result.nil? end def result_params params.require(:result).permit(:result_value, :result_date) end end 

索引会像这样转变,每个名称(指标)都有多个结果和日期:

在此处输入图像描述

我尝试实现这些说明但我不断变化有不同的错误: http : //archive.railsforum.com/viewtopic.php?id = 717

我也尝试了这个教程: http : //railscasts.com/episodes/197-nested-model-form-part-2 。 但它有点过时,所以我得到了一堆错误。

在此先感谢您给我的任何帮助!

你想要做的是NestedAttributes,你可以阅读更多关于它的信息http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

例子:

 # creates avatar_attributes= accepts_nested_attributes_for :avatar, reject_if: proc { |attributes| attributes['name'].blank? } # creates avatar_attributes= accepts_nested_attributes_for :avatar, reject_if: :all_blank # creates avatar_attributes= and posts_attributes= accepts_nested_attributes_for :avatar, :posts, allow_destroy: true 

这是类定义

 # File activerecord/lib/active_record/nested_attributes.rb, line 301 def accepts_nested_attributes_for(*attr_names) options = { :allow_destroy => false, :update_only => false } options.update(attr_names.extract_options!) options.assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only) options[:reject_if] = REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank attr_names.each do |association_name| if reflection = _reflect_on_association(association_name) reflection.autosave = true add_autosave_association_callbacks(reflection) nested_attributes_options = self.nested_attributes_options.dup nested_attributes_options[association_name.to_sym] = options self.nested_attributes_options = nested_attributes_options type = (reflection.collection? ? :collection : :one_to_one) generate_association_writer(association_name, type) else raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?" end end end 

此外,如果你需要更多,有一个非常酷的video来自rails cast演示解释它http://railscasts.com/episodes/196-nested-model-form-part-1