我正在尝试扩展设计注册表单,以允许新用户在注册的同时创建组织(公司)。我终于可以看到嵌套的表单字段了(在阅读了几个SO线程之后),但是我遇到了一个错误,我认为它与批量分配有关。
我是一个完整的铁杆菜鸟,仍然让我的脚湿了。其他SO问题让我走到了这一步,但现在我陷入困境。
以下是我当前的文件:
用户模型:
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
validates_presence_of :first_name, :last_name
validates_length_of :first_name, :maximum => 50
validates_length_of :last_name, :maximum => 50
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :first_name, :last_name, :password_confirmation, :remember_me, :organization_attributes, :organization
end
组织模式
class Organization < ActiveRecord::Base
has_many :users, :foreign_key => "user_id"
accepts_nested_attributes_for :users
end
注册表
<h2>Register for a new account</h2>
<% resource.build_organization %>
<%= form_for(resource, :html => { :class => 'form-horizontal' }, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<fieldset>
<legend class="hidden">Sign Up Form</legend>
<h3>User Information</h3>
<div class="control-group">
<%= f.label :first_name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :first_name %>
</div>
</div>
<div class="control-group">
<%= f.label :last_name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :last_name %>
</div>
</div>
<div class="control-group">
<%= f.label :email, :class => 'control-label' %>
<div class="controls">
<%= f.email_field :email %>
</div>
</div>
<div class="control-group">
<%= f.label :password, :class => 'control-label' %>
<div class="controls">
<%= f.password_field :password %>
</div>
</div>
<div class="control-group">
<%= f.label :password_confirmation, :class => 'control-label' %>
<div class="controls">
<%= f.password_field :password_confirmation %>
</div>
</div>
<h3>Company/Organization Information</h3>
<%= f.fields_for resource.organization do |fo| %>
<div class="control-group">
<%= fo.label :name, :class => 'control-label' %>
<div class="controls">
<%= fo.text_field :name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= fo.label :subdomain, :class => 'control-label' %>
<div class="controls">
<%= fo.text_field :subdomain, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= fo.label :plan, :class => 'control-label' %>
<div class="controls">
<%= fo.text_field :plan, :class => 'text_field' %>
</div>
</div>
<% end %>
<div class="form-actions">
<%= f.submit "Sign up", :class => 'btn btn-primary' %>
<%= link_to 'Forgot Password?', :new_user_password, :class => 'btn btn-info' %>
</div>
</fieldset>
<% end %>
错误消息
ActiveRecord::AssociationTypeMismatch in Devise::RegistrationsController#create
Organization(#70312257412900) expected, got ActiveSupport::HashWithIndifferentAccess(#70312251932740)
Rails.root: /Volumes/www/projects/ror/testapp
Application Trace | Framework Trace | Full Trace
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"SG/jlH2L3ATSqi1lBxlgbvWzx/sHVFlneX8vF/LKKZg=",
"user"=>{"first_name"=>"",
"last_name"=>"",
"email"=>"",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"organization"=>{"name"=>"",
"subdomain"=>"",
"plan"=>""}},
"commit"=>"Sign up"}
我确信我错过了一些简单的事情。期待看到它是什么;)
答案 0 :(得分:2)
试试这个:
f.fields_for :organization_attributes, resource.organization do |fo|