我正在学习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我在铁路广播上使用了嵌套模型表单的第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我的表格是:
<%= 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 %>但是外公的名字不会起作用。我找不到错误的...anyone来帮助解决此问题?谢谢!
发布于 2013-11-25 07:28:28
尝试切换:
<%= f.fields_for :grandfather do |fff| %>至:
<%= 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]])https://stackoverflow.com/questions/20182019
复制相似问题