Пытаюсь разобраться со следующей ситуацией.
рутина.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 #/подпрограммы/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"}`
Не смотрите прямо. Он должен ссылаться на training_routines с идентификатором.
2) Хотите иметь возможность добавить попытку одновременно.
@routine.exercise_routines.each do |er|
1.times {er.attempts.build}
end
Отображает это правильно, но, конечно, я сталкиваюсь с той же проблемой параметров. Я либо делаю что-то глупо неправильно, либо это сложнее, чем я думаю, для такого типа вложения нескольких коллекций?