如何在rails中保存包含其他模型属性的模型?

在我的项目中,我有一个Organization模型和一个Address模型。 以下是模型之间的关联:

 class Organization < ApplicationRecord has_one :address accepts_nested_attributes_for :address end class Address < ApplicationRecord belongs_to :organization end 

我在我的新组织表单中添加了这样的地址属性(form_with用于Organization属性,fields_for用于Address属性):

  
Street number:
Street:
City:
Province:
Postal code:

当我试图用他的地址保存组织时,组织被保存,但他的地址不是。

如何保存组织的地址?

这是我的OrganizationController:

 def new @organization = Organization.new @organization.build_address end def create @organization = Organization.new(organization_params) @organization.save //... end def organization_params params.require(:organization).permit(:organizationName, :email, :webSite, address_attributes:[:streetNumber, :street, :city, :province, :postalCode]) end 

编辑

问题是我的看法。 我的表单中没有包含我的field_for部分。

解:

  

  belongs_to :address, optional: true params.require(:organization).permit(:name,address_attributes: [:id,:city])