我一直在關注RailsCast 197來試試這個嵌套的模型/表單,並已經破解了我的頭超過這個代碼超過2小時,但無濟於事。我忽略了什麼?嵌套模型拋出未定義的方法錯誤
我有以下型號:
class Workout < ActiveRecord::Base
belongs_to :user
has_many :performed_exercises, :dependent => :destroy
accepts_nested_attributes_for :performed_exercises
end
class PerformedExercise < ActiveRecord::Base
belongs_to :workout
belongs_to :exercise
has_many :performed_sets, :dependent => :destroy
accepts_nested_attributes_for :performed_sets
end
class PerformedSet < ActiveRecord::Base
belongs_to :performed_exercise
end
在我WorkoutsController我有以下幾點:
def new
# We only need to build one of each since they will be added dynamically
@workout = Workout.new
@workout.performed_exercises.build
@workout.performed_exercises.performed_sets.build
end
當我運行測試,並在瀏覽器中調用控制器,我得到以下錯誤:
undefined method `performed_sets' for #<Class:0x7f6ef6fa6560>
在此先感謝您的幫助 - 我的RoR noobility不再讓我驚歎!
編輯: fflyer05:我嘗試使用相同的代碼RailsCast與遍歷集合,並試圖建立在performed_exercises的performed_sets [0] - 但它不工作。做任何事情,我得到一個未初始化的常量PerformedExercise :: PerformedSet錯誤
非常感謝soooo fflyer05。我的PerformedSet模型文件名是perform_sets。 – MunkiPhD 2010-08-29 14:50:00