【问题标题】:Link in Nested Forms returning Invalid Association嵌套表单中的链接返回无效关联
【发布时间】:2013-05-14 22:29:43
【问题描述】:

我正在使用嵌套表单为与其关联的一组模型创建一个复杂的表单列表。但是,当我在表单中添加 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

【问题讨论】:

  • 请发布您的 client_workout 模型
  • 发布有问题。

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


【解决方案1】:

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

= nested_form_for(@workout) do |f|

= f.fields_for :client_workouts do |clientworkout_form|

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多