Javascript 如何在一个表单中包含两个模型?
一个模型创建:name、:metric(Quantified.rb),另一个模型创建:result\u value、:result\u date(result.rb)Javascript 如何在一个表单中包含两个模型?,javascript,ruby-on-rails,ruby,forms,model,Javascript,Ruby On Rails,Ruby,Forms,Model,一个模型创建:name、:metric(Quantified.rb),另一个模型创建:result\u value、:result\u date(result.rb) class{where(类别:'月平均')} 作用域:实例,->{where(类别:“一次性实例”)} 有很多:结果 类别=['月平均值','一次实例'] 结束 类结果
class{where(类别:'月平均')}
作用域:实例,->{where(类别:“一次性实例”)}
有很多:结果
类别=['月平均值','一次实例']
结束
类结果 结束
您试图做的是嵌套属性您可以阅读更多关于它的信息
示例:
# 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
另外,如果您需要更多,rails cast会提供一个非常酷的视频来解释这一点