Я пытаюсь ознакомиться со следующей ситуацией.
routine.rb
class Routine < ActiveRecord::Base
has_many :exercise_routines
has_many :exercises, :through => :exercise_routines
accepts_nested_attributes_for :exercise_routines
end
exercise_routine.rb
class ExerciseRoutine < ActiveRecord::Base
belongs_to :exercise
belongs_to :routine
has_many :attempts
accepts_nested_attributes_for :attempts
end
show.html # / routines / show.html.haml
%h2 Exercises:
%ol
- for exercise_routine in @routine.exercise_routines
%li= exercise_routine.exercise.name
%ul
%li
= exercise_routine.sets
Sets
%li
= exercise_routine.reps
Reps
%li
Attempt:
- for attempt in exercise_routine.attempts
= attempt.reps
= semantic_form_for @routine do |routine|
= routine.semantic_fields_for exercise_routine do |exercise_routine|
= exercise_routine.semantic_fields_for :attempts do |attempt|
= attempt.input :reps, :required => false
= routine.buttons`
У меня проблемы с двумя вещами:
1) Это успешно рендеринг попыток для каждой подпрограммы, если я вручную добавлю ее через консоль, но если я попытаюсь обновить ее, я получу: «неизвестный атрибут: упражнение_программа», и я это понимаю, потому что параметры:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"51fZRe1wKRQoVUz+lGlldd4DmMODGHN203Htc4DEi0Y=",
"routine"=>{"exercise_routine"=>{"attempts_attributes"=>{"0"=>{"reps"=>"5",
"id"=>"1"},
"1"=>{"reps"=>"1555",
"id"=>"2"},
"2"=>{"reps"=>""}}}},
"commit"=>"Update Routine",
"id"=>"3"}`
Не смотри правильно. Он должен ссылаться на упражнение с идентификатором.
2) Хотите иметь возможность добавить попытку одновременно.
@routine.exercise_routines.each do |er|
1.times {er.attempts.build}
end
Отрисовывает это правильно, но, конечно, я сталкиваюсь с той же проблемой params. Я либо делаю что-то глупо неправильно, либо это сложнее, чем я думаю, чтобы сделать этот тип вложенности нескольких коллекций?