嵌套表单中的链接返回无效关联
Posted
技术标签:
【中文标题】嵌套表单中的链接返回无效关联【英文标题】: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 模型 发布有问题。 【参考方案1】:你应该把它倒过来,因为workout
有很多workout_clients
= nested_form_for(@workout) do |f|
和
= f.fields_for :client_workouts do |clientworkout_form|
【讨论】:
以上是关于嵌套表单中的链接返回无效关联的主要内容,如果未能解决你的问题,请参考以下文章