10

Sto imparando RoR e sto cercando di trovare il modo di impostare un fields_for in un altro con modelli has_one come questo:Rails 4: fields_for in 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 

ho usato Nested Modello Modulo Part 1 Railscasts a ottenere questi: In 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 

E la mia 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 %> 

Sto cercando di recuperare i dati con:

ma il nome del nonno non funzionerà. Non riesco a trovare l'errore (s) ... qualcuno per aiutare su questo? Grazie! commutazione

risposta

15

Prova:

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

a:

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

e switching:

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

A:

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

funziona perfettamente! Grazie mille! – user3029400

Problemi correlati