2015-01-06 87 views
1

我有以下类:Rails的作用域相关联瓦特/嵌套形式

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:

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的内部工作方式来调试它。任何想法我做错了什么?

回答

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 
+0

我不认为这是问题所在。如果是这样,我是不是有一个问题来渲染表单或获得未经许可的params错误? –