我在设计的自定义注册中遇到了表单问题。此表单需要同时包含用户和业务模型信息,我将在表单上同时创建它们。重要的是,这不能是一个多步骤的注册表单 - 作为要求的一部分,我需要同时填写用户模型和业务模型。
我有以下型号:
user.rb
class User < ActiveRecord::Base
has_many :businessusers, :include => :business
has_many :businesses, :through => :businessusers
accepts_nested_attributes_for :businessusers
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :role, :businessusers
business.rb
class Business < ActiveRecord::Base
has_many :businessusers
has_many :users, :through => :businessusers
businessuser.rb
class Businessuser < ActiveRecord::Base
belongs_to :business
belongs_to :user
end
businessusers migration
create_table :businessusers do |t|
t.integer :user_id
t.integer :business_id
end
在我的注册表单中,我有以下代码:
Business Information
<br>
<%= f.fields_for :businessusers do |business_form| %>
<%= business_form.input :name %>
<%= business_form.input :address_line1 %>
<%= business_form.input :address_line2 %>
<%= business_form.input :city %>
最后,这是我的控制器:
custom_registration_controller.rb
def create
@user=User.new(params[:user])
@business = @user.businesses.build(params[:business]) unless params[:business] [:name].blank?
end
编辑:我已经解决了质量分配属性问题,但现在我有另一个问题。
有谁知道我要去哪里错了?之前,我在 businessusers 表上禁用了 id。但这给了我一个错误,说我需要桌子上的主 ID。因此,当我允许该字段时:id
,它会使表单不再具有业务字段。
谢谢!