ruby-on-rails – Rails 4:fields_for in fields_for
发布时间:2020-12-16 22:20:29 所属栏目:百科 来源:网络整理
导读:我正在学习RoR,我试图找到如何使用has_one模型设置另一个fields_for: class Child ActiveRecord::Base belongs_to :father accepts_nested_attributes_for :fatherendclass Father ActiveRecord::Base has_one :child belongs_to :grandfather accepts_nest
我正在学习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 我在Railscast上使用嵌套模型表格第1部分来获取以下内容: 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 %> 但祖父的名字不会奏效. 解决方法
尝试切换:
<%= f.fields_for :grandfather do |fff| %> 至: <%= ff.fields_for :grandfather do |fff| %> 切换: params.require(:child).permit(:name,grandfather_attributes:[:name]) 至: params.require(:child).permit(:name,father_attributes:[:name,grandfather_attributes:[:name]]) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |