Tag: fields for

绝对坚持尝试使用has_many来创建railsforms的嵌套关联

我之前发布了一个关于此的问题,并被建议阅读大量相关信息。 我已阅读并尝试实施约30种不同的解决方案。 这些都不适合我。 这就是我所拥有的。 我有一个Miniatures模型。 我有制造商型号。 Miniatures有许多制造商通过Productions模型。 这些关联似乎设置正确,因为我可以在我的视图中显示它们并通过控制台创建它们。 我遇到问题的方法是让Miniatures NEW和EDIT视图创建并更新到Productions表。 在控制台中,命令@miniature.productions.create(manufacturer_id: 1)起作用,这使我相信我应该能够在表单中执行相同的操作。 我认为我的问题总是在Miniatures Controller中,特别是CREATE函数。 我在那里尝试了很多其他人的解决方案,没有人能做到这一点。 我的field_forforms也可能是错误的,但这似乎不那么繁琐。 我已经坚持了几天,虽然还有其他我可以继续工作的事情,如果这种关联不可能,那么我需要重新考虑我的整个应用程序。 表单现在在Productions表中创建一行,但不包括所有重要的manufacturer_id。 任何帮助非常感谢。 我的新微缩forms Add a miniature Date.current.year, :end_year => 1970, :include_blank => true %> 微型控制器 class MiniaturesController < ApplicationController before_action :signed_in_user, only: [:new, :create, :edit, :update] before_action :admin_user, only: :destroy def productions @production = @miniature.productions end def show […]

Rails 4:fields_for中的fields_for

我正在学习RoR,我正在尝试找到如何使用has_one模型在另一个中设置fields_for: class Child < ActiveRecord::Base belongs_to :father accepts_nested_attributes_for :father end class Father < ActiveRecord::Base has_one :child belongs_to :grandfather accepts_nested_attributes_for :grandfather end class Grandfather < ActiveRecord::Base has_one :father end 我在Railscasts上使用嵌套模型表单第1部分来获取这些:在children_controller.rb中: def new @child = Child.new father=@child.build_father father.build_grandfather end def child_params params.require(:child).permit(:name, father_attributes:[:name], grandfather_attributes:[:name]) end 我的表格: mother: grand mother: 我试图用以下方法检索数据: 但爷爷的名字不会起作用。 我找不到错误……有人帮忙吗? 谢谢!