嵌套表单中的链接返回无效关联

时间:2013-05-14 22:29:43

标签: ruby-on-rails ruby-on-rails-3 forms associations nested-forms

我正在使用嵌套表单为与之关联的一组模型创建复杂的表单列表。但是,当我在表单中添加link_to_add链接时,它会返回错误消息

无效关联。确保accepts_nested_attributes_for用于:workout_exercises association。

如果删除链接,一切正常。我必须假设它是一个视图错误,因为删除它不会导致任何问题,如果我的关联或接受的嵌套表单不起作用,我想会提出一个问题。我的代码出了什么问题,如何修复它。

= 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

1 个答案:

答案 0 :(得分:0)

您应该将其反转,因为workout有许多workout_clients

= nested_form_for(@workout) do |f|

= f.fields_for :client_workouts do |clientworkout_form|