我有以下课程:
class Business < AR::Base
has_many :locations
has_many :owned_locations, -> { owned }, class_name: 'Location'
has_many :good_locations, -> { good }, class_name: 'Location'
has_many :bad_locations, -> { bad }, class_name: 'Location'
accepts_nested_attributes_for :locations, allow_destroy: true
accepts_nested_attributes_for :owned_locations, allow_destroy: true
accepts_nested_attributes_for :good_locations, allow_destroy: true
accepts_nested_attributes_for :bad_locations, allow_destroy: true
end
class Location < AR::Base
belongs_to :business
scope :owned, -> { where(type: 'owned') }
scope :good, -> { where(type: 'good') }
scope :bad, -> { where(type: 'bad') }
end
基本上我尝试建立一个关联,其中locations
可以分配给多个存储桶(类型),但都存储在同一个表中。我遇到了嵌套表单的问题:
= simple_form_for @business do |f|
= f.fields_for :owned_locations, @business.owned_locations do |lf|
# location fields
我的控制器有适当的允许参数:
params.permit(
:business => [
:name,
:owned_locations_attributes => [
# locations_attributes
]
]
)
我没有得到任何未经许可的params错误,所以我确信我已经正确设置了所有这些错误。但这些地点并没有被分配到这个行业。这些是错误:
{
:"owned_locations.business"=>["can't be blank"],
:"good_locations.business"=>["can't be blank"],
:"bad_locations.business"=>["can't be blank"]
}
我不太了解Rails API的内部工作原理来调试它。知道我做错了吗?
答案 0 :(得分:1)
当您使用simple_form创建嵌套表单时,需要使用simple_fields_for。
= simple_form_for @business do |f|
= f.simple_fields_for :owned_locations, @business.owned_locations do |lf|
# location fields