text_field rails form for fields_for accepts_nested_attributes_for ruby-on-rails ruby-on-rails-4 form-for fields-for nested-form-for

ruby on rails - form - Rails 4: fields_for en fields_for



nested form rails (1)

Estoy aprendiendo RoR y estoy tratando de encontrar cómo configurar un fields_for en otro con modelos has_one como este:

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

Utilicé el Formulario de Modelo Anidado Parte 1 en Railscasts para obtener estos: En children_controller.rb:

def new @child = Child.new [email protected]_father father.build_grandfather end def child_params params.require(:child).permit(:name, father_attributes:[:name], grandfather_attributes:[:name]) end

Y mi forma:

<%= 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 %>

Estoy tratando de recuperar los datos con:

<%= child.father.name %> <%= child.father.grandfather.name %>

pero el nombre del abuelo no funcionará. No puedo encontrar el (los) error (s) ... ¿alguien para ayudar con esto? ¡Gracias!


Intenta cambiar:

<%= f.fields_for :grandfather do |fff| %>

a:

<%= ff.fields_for :grandfather do |fff| %>

Y cambiando:

params.require(:child).permit(:name, father_attributes:[:name], grandfather_attributes:[:name])

A:

params.require(:child).permit(:name, father_attributes:[:name, grandfather_attributes:[:name]])