我正在尝试将一组用户添加到company_profile对象中。这个想法是用户将创建一个公司,然后以不同的角色向公司添加更多用户。
公司资料有一个地址对象,当我在新电话上拉表格时,我收到此错误:
“无法写出未知属性company_profile_id
”
company_profile - >新
<%= form_for(setup_companyProfile(@companyProfile), validate: true, html: { multipart: true }) do |f| %>
<%= f.fields_for :address do |address| %>
<%= render :partial => 'shared/address', :locals => {:f => address} %>
<% end %>
<% end %>
user.rb
belongs_to :company_profile
helper.rb
def setup_companyProfile(companyProfile)
if(companyProfile.address.present? == false)
companyProfile.address ||= Address.new
end
companyProfile
end
company_profile.rb
class CompanyProfile < ApplicationRecord
has_many :users
has_one :address
accepts_nested_attributes_for :address
end
company_profile_controller.rb
class CompanyProfileController < ApplicationController
def new
@companyProfile = CompanyProfile.new
end
def edit
@companyProfile = CompanyProfile.find(current_user.company_profile_id)
end
def update
end
def show
end
end
答案 0 :(得分:0)
当您使用公司资料模型中的has_one
时... Rails预计belongs_to :company_profile
模型上会有Address
...并且此belongs_to
要求company_profile_id
表上有一个名为addresses
的列...你有吗?如果不是 - 您将需要创建一个添加它的迁移。