0

我正在使用嵌套窗體爲與之關聯的一組模型創建窗體的複雜列表。但是,當我在表單中添加link_to_add鏈接時,它會返回錯誤消息,返回無效關聯的嵌套窗體中的鏈接

無效的關聯。確保將accep_nested_attributes_for用於:workout_exercises關聯。

如果我刪除鏈接一切工作正常。我必須假設它是一個視圖錯誤,因爲刪除它不會導致任何問題,如果我的關聯或接受的嵌套表單無法正常工作,我想會引發一個問題。我的代碼有什麼問題,我該如何解決它。

= nested_form_for(@client_workout) do |f| 

    = f.number_field :client_id, placeholder: "Client's Name" 

    = f.text_field :description, placeholder: "description" 

    = f.text_field :note, placeholder: "notes" 

    = f.fields_for :workouts do |workout_form| 
    = workout_form.text_field :name, placeholder: "Workout Name" 

    = workout_form.fields_for :workout_exercises do |exercise_details_form| 
     div Create an Exercise 

     = exercise_details_form.fields_for :exercise do |exercise_form| 
     = exercise_form.text_field :name, placeholder: "Exercise Name" 

     = exercise_details_form.text_field :reps, placeholder: "reps" 

     = exercise_details_form.text_field :sets, placeholder: "sets" 

     = exercise_details_form.text_field :weight, placeholder: "weight" 

     = exercise_details_form.text_field :category, placeholder: "category" 

     = exercise_details_form.link_to_remove "Remove this exercise", class: "btn btn-danger" 

    /This line of code below is causing the problem 
    = workout_form.link_to_add "Add this exercise", :workout_exercises 

    = f.submit class: "btn btn-success" 

client_workout模型

class ClientWorkout < ActiveRecord::Base 
    attr_accessible :workout_id, :description, :note 

    belongs_to :workout 

    accepts_nested_attributes_for :workout 
end 
+0

請發表您的client_wo rkout模型 –

+0

發佈有問題。 – jason328

回答

0

你應該顛倒,因爲workout有很多workout_clients

= nested_form_for(@workout) do |f| 

而且

= f.fields_for :client_workouts do |clientworkout_form|