Has_many accepts_nested_attributes无法批量分配属性

时间:2012-08-03 15:44:18

标签: ruby-on-rails ruby-on-rails-3 devise

我在Devise的自定义注册中遇到表单问题。此表单需要同时包含用户和商业模型信息,我将在表单上同时创建这些信息。同样重要的是,这不能是一个多步骤的注册表 - 我需要同时填写用户和商业模型,作为要求的一部分。

我有以下型号:

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时,它会使表单不再包含业务字段。

谢谢!

0 个答案:

没有答案