Подтвердить что ты не робот

Rails 4: fields_for in fields_for

Я изучаю RoR, и я пытаюсь найти, как установить field_for в другом с помощью 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_attributes_for :grandfather
end


class Grandfather < ActiveRecord::Base
    has_one :father
end

Я использовал форму 1 для вложенной модели в Railscasts, чтобы получить: В child_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

И моя форма:

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

но имя деда не сработает. Я не могу найти ошибку (ы)... кто-нибудь, чтобы помочь в этом? Спасибо!

4b9b3361

Ответ 1

Попробуйте переключиться:

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

to:

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

И переключение:

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

To:

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