我有一個類地址,其中Organismereferent和Organisme有has_many
belongs_to
的關係。也Organismereferent和Organisme可以accepts_nested_attributes_for :addresses
兩個不同的類使用一個類的accepted_nested_attributes
我的問題是,當我只有Organismereferent類一切工作正常,我能夠創建一個新的Organismereferent地址,但只要我創建並添加了相同的關係Organisme,他們都停不給任何錯誤信息的工作,我只在控制檯中看到這一點:
我的模型:
class Organismereferent < ApplicationRecord
has_many :addresses
has_many :referents
accepts_nested_attributes_for :addresses
end
class Organisme < ApplicationRecord
has_many :addresses
accepts_nested_attributes_for :addresses
end
class Address < ApplicationRecord
belongs_to :organismereferent
belongs_to :organisme
end
控制器Organisme
def new
@organisme = Organisme.new
@organisme.addresses.build
end
def create
@organisme = Organisme.new(organisme_params)
@organisme.status = true
@organisme.save
redirect_to @organisme
end
private
def organisme_params
params.require(:organisme).permit(:nom, :telephone, :courriel, :fax, addresses_attributes: [:id, :no_civique, :rue, :ville, :province, :etat, :code_postal])
end
控制器Organismereferent:
def new
@organisme = Organismereferent.new
@organisme.addresses.build
end
def create
@organisme = Organismereferent.new(organisme_params)
@organisme.active = true
@organisme.save
redirect_to @organisme
end
private
def organisme_params
params.require(:organismereferent).permit(:nom_organisation, :bureau, :telecopie, :courriel, :site_web, addresses_attributes: [:id, :no_civique, :rue, :ville, :province, :etat, :code_postal])
end
我不知道還有什麼信息可能是重要的,所以我會很高興來添加任何東西。
你能告訴我們錯誤消息的地址模式? '@ organisme.save!'會顯示錯誤信息。 –
@JunanChakma它給了我這個'驗證失敗:地址organisme必須存在' – CNuts