0

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

无效的关联。确保accepts_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
4

1 回答 1

0

你应该把它倒过来,因为workout有很多workout_clients

= nested_form_for(@workout) do |f|

= f.fields_for :client_workouts do |clientworkout_form|
于 2013-05-15T01:10:06.310 回答