I am trying to expand the devise registration form to allow a new user to create an organization (company) at the same time they sign up. I finally got the nested form fields visible (after reading several SO threads), but I have encountered an error that I believe to be associated with mass assignment.
I am a complete rails noob, still getting my feet wet. Other SO questions have gotten me this far, but now I am just stuck.
Here are my current files:
User Model:
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
Organization Model
class Organization < ActiveRecord::Base
has_many :users, :foreign_key => "user_id"
accepts_nested_attributes_for :users
end
Registration Form
<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 %>
Error Message
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"}
I am sure I am missing something simple. Looking forward to seeing what it is 😉
try this: