Rails 复杂关联的嵌套形式
Rails nested form for complex associations
问题:
=rfv.text_area :value
在编辑操作中呈现无效文本区域(但在数据库中此字段具有值)
- 无法在 params
中获取 role_field_values[] 的数组
我的模型和关联:
class Participant < ActiveRecord::Base
belongs_to :role
has_many :role_field_values, dependent: :destroy
accepts_nested_attributes_for :role_field_values
end
class Role < ActiveRecord::Base
has_many :role_fields
has_one :participant
end
class RoleField < ActiveRecord::Base
belongs_to :role
has_many :role_field_values
end
class RoleFieldValue < ActiveRecord::Base
belongs_to :participant
belongs_to :role_field
end
我的表格edit.slim:
=simple_form_for [@project.becomes(Project), @participant] do |f|
=f.error_notification
h4 =@participant.profile.fio
.form-inline
=f.association :role, remote: true
=f.input :status
#role-fields
=fields_for :role_fields_values do |rfv|
-@participant.role.role_fields.each do |role_field|
.form-group
=rfv.hidden_field :role_field_id, value: role_field.id
br
=rfv.label role_field.name
=rfv.text_area :value, class: 'form-control'
.form-actions
= f.button :submit
使用f.fields_for代替fields_for
问题:
=rfv.text_area :value
在编辑操作中呈现无效文本区域(但在数据库中此字段具有值)- 无法在 params 中获取 role_field_values[] 的数组
我的模型和关联:
class Participant < ActiveRecord::Base
belongs_to :role
has_many :role_field_values, dependent: :destroy
accepts_nested_attributes_for :role_field_values
end
class Role < ActiveRecord::Base
has_many :role_fields
has_one :participant
end
class RoleField < ActiveRecord::Base
belongs_to :role
has_many :role_field_values
end
class RoleFieldValue < ActiveRecord::Base
belongs_to :participant
belongs_to :role_field
end
我的表格edit.slim:
=simple_form_for [@project.becomes(Project), @participant] do |f|
=f.error_notification
h4 =@participant.profile.fio
.form-inline
=f.association :role, remote: true
=f.input :status
#role-fields
=fields_for :role_fields_values do |rfv|
-@participant.role.role_fields.each do |role_field|
.form-group
=rfv.hidden_field :role_field_id, value: role_field.id
br
=rfv.label role_field.name
=rfv.text_area :value, class: 'form-control'
.form-actions
= f.button :submit
使用f.fields_for代替fields_for