Ruby on rails Rails 4:in-fields的字段
我正在学习RoR,并试图找到如何在另一个has_one模型中设置字段,如下所示:Ruby on rails Rails 4:in-fields的字段,ruby-on-rails,ruby-on-rails-4,form-for,fields-for,nested-form-for,Ruby On Rails,Ruby On Rails 4,Form For,Fields For,Nested Form For,我正在学习RoR,并试图找到如何在另一个has_one模型中设置字段,如下所示: 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_
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
我的表格:
<%= form_for(@child) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
mother:<br>
<%= f.fields_for :father do |ff| %>
<%= ff.label :name %>
<%= ff.text_field :name %><br>
grand mother:<br>
<%= f.fields_for :grandfather do |fff| %>
<%= fff.label :name %>
<%= fff.text_field :name %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
母亲:
祖母:
我正在尝试检索以下数据:
<%= child.father.name %>
<%= child.father.grandfather.name %>
但是祖父的名字不起作用。
我找不到错误…有人可以帮忙吗?
谢谢 尝试切换:
<%= f.fields_for :grandfather do |fff| %>
params.require(:child).permit(:name, father_attributes:[:name], grandfather_attributes:[:name])
致:
<%= ff.fields_for :grandfather do |fff| %>
params.require(:child).permit(:name, father_attributes:[:name], grandfather_attributes:[:name])
params.require(:child).permit(:name, father_attributes:[:name, grandfather_attributes:[:name]])