मेरा मानना है कि रजिस्ट्रेशंसकंट्रोलर को फिर से लिखने से बेहतर उपाय है। मैंने बिल्कुल वही काम किया (कंपनी के बजाय मेरे पास संगठन है)।
यदि आप मॉडल और दृश्य स्तर पर अपना नेस्टेड रूप ठीक से सेट करते हैं, तो सब कुछ एक आकर्षण की तरह काम करता है।
मेरा उपयोगकर्ता मॉडल:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :owned_organizations, :class_name => 'Organization', :foreign_key => :owner_id
has_many :organization_memberships
has_many :organizations, :through => :organization_memberships
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :username, :owned_organizations_attributes
accepts_nested_attributes_for :owned_organizations
...
end
मेरा संगठन मॉडल:
class Organization < ActiveRecord::Base
belongs_to :owner, :class_name => 'User'
has_many :organization_memberships
has_many :users, :through => :organization_memberships
has_many :contracts
attr_accessor :plan_name
after_create :set_owner_membership, :set_contract
...
end
मेरा विचार: 'वसीयत / पंजीकरण / new.html.erb'
<h2>Sign up</h2>
<% resource.owned_organizations.build if resource.owned_organizations.empty? %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
<p><%= f.label :email %><br />
<%= f.text_field :email %></p>
<p><%= f.label :username %><br />
<%= f.text_field :username %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<%= f.fields_for :owned_organizations do |organization_form| %>
<p><%= organization_form.label :name %><br />
<%= organization_form.text_field :name %></p>
<p><%= organization_form.label :subdomain %><br />
<%= organization_form.text_field :subdomain %></p>
<%= organization_form.hidden_field :plan_name, :value => params[:plan] %>
<% end %>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>