1
我想讓自己熟悉以下情況。Formtastic嵌套幫助
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#/程序/ 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)這是成功地爲每個例程渲染嘗試,如果我手動添加thro唉控制檯,但如果我嘗試更新它,我會得到:「未知屬性:exercise_routine」我意識到,由於PARAMS:
{"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"}`
看起來不正確。它應該用一個id引用exercise_routines。
2)希望能夠同時添加嘗試。
@routine.exercise_routines.each do |er|
1.times {er.attempts.build}
end
正確渲染它,但當然我遇到了相同的參數問題。我要麼做一些愚蠢的錯誤,要麼這比我認爲要做這種多集合嵌套更難?