Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe there is a better solution than rewrite the RegistrationsController. I did exactly the same thing (I just have Organization instead of Company).</p> <p>If you set properly your nested form, at model and view level, everything works like a charm.</p> <p>My User model:</p> <pre><code>class User &lt; 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 =&gt; 'Organization', :foreign_key =&gt; :owner_id has_many :organization_memberships has_many :organizations, :through =&gt; :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 </code></pre> <p>My Organization Model:</p> <pre><code>class Organization &lt; ActiveRecord::Base belongs_to :owner, :class_name =&gt; 'User' has_many :organization_memberships has_many :users, :through =&gt; :organization_memberships has_many :contracts attr_accessor :plan_name after_create :set_owner_membership, :set_contract ... end </code></pre> <p>My view : 'devise/registrations/new.html.erb'</p> <pre><code>&lt;h2&gt;Sign up&lt;/h2&gt; &lt;% resource.owned_organizations.build if resource.owned_organizations.empty? %&gt; &lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name)) do |f| %&gt; &lt;%= devise_error_messages! %&gt; &lt;p&gt;&lt;%= f.label :name %&gt;&lt;br /&gt; &lt;%= f.text_field :name %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.label :email %&gt;&lt;br /&gt; &lt;%= f.text_field :email %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.label :username %&gt;&lt;br /&gt; &lt;%= f.text_field :username %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.label :password %&gt;&lt;br /&gt; &lt;%= f.password_field :password %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.label :password_confirmation %&gt;&lt;br /&gt; &lt;%= f.password_field :password_confirmation %&gt;&lt;/p&gt; &lt;%= f.fields_for :owned_organizations do |organization_form| %&gt; &lt;p&gt;&lt;%= organization_form.label :name %&gt;&lt;br /&gt; &lt;%= organization_form.text_field :name %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= organization_form.label :subdomain %&gt;&lt;br /&gt; &lt;%= organization_form.text_field :subdomain %&gt;&lt;/p&gt; &lt;%= organization_form.hidden_field :plan_name, :value =&gt; params[:plan] %&gt; &lt;% end %&gt; &lt;p&gt;&lt;%= f.submit "Sign up" %&gt;&lt;/p&gt; &lt;% end %&gt; &lt;%= render :partial =&gt; "devise/shared/links" %&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload