Deep Nested Rails 4 Form
Mam 3 modele Element, który akceptuje zagnieżdżone atrybuty dla pytań i pytań, akceptuje zagnieżdżone atrybuty odpowiedzi. Próbuję stworzyć przedmiot, który ma pytanie i odpowiedź w tej samej formie.
item.rb
class Item < ActiveRecord::Base
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions
end
pytanie.rb
class Question < ActiveRecord::Base
belongs_to :item
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers
end
answer.rb
class Answer < ActiveRecord::Base
belongs_to :question
end
item_controller.rb
class ItemsController < ApplicationController
def new
@item = @repository.items.new
questions = @item.questions.build
answers = questions.answers.build
end
def create
@item = Item.new(item_params)
if @item.save
redirect_to @item, notice: '...'
else
render action: 'new'
end
end
private
def item_params
params.require(:item).permit(:id, :content, :kind, :questions_attributes => [:content, :helper_text, :kind], :answers_attributes => [:content, :correct])
end
end
_form.haml
= simple_form_for(@item) do |f|
= f.input :kind
= f.input :content
= f.simple_fields_for :questions do |q|
= q.input :content
= q.simple_fields_for :answers do |a|
= a.input :content
= f.submit
Formularz jest wyświetlany poprawnie ipoprawnie zapisuje model pytania. Nie mogę zapisać odpowiedzi chociaż.
Sprawdziłem już wiele pomocy online, ale żadna z nich nie poradziła sobie z 4 silnymi parametrami Rails.