数日前に同じ問題がありました。うまくいく解決策を見つけました!
# models/company.rb
class Company < ActiveRecord::Base
has_many :users, :dependent => :destroy
validates :subdomain, :presence => true,
:uniqueness => { :case_sensitive => false },
:length => { :within => 4..20 },
:format => { :with => /^[a-z0-9]+$/i }
attr_accessible :name, :subdomain
end
# ======================================================================================
# models/user.rb
class User < ActiveRecord::Base
before_create :create_company
belongs_to :company
validates :subdomain, :on => :create,
:presence => true,
:length => { :within => 4..20 },
:format => { :with => /^[a-z0-9]+$/i }
validates_presence_of :nome
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable,
:authentication_keys => [:subdomain]
attr_accessor :subdomain # VIRTUAL ATTRIBUTE
attr_accessible :name, :email, :subdomain, :password, :password_confirmation,
:remember_me, :loginable_token
private
def create_company
self.company = Company.create!(:subdomain => self.subdomain)
end
end
# ======================================================================================
# example of registration form
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
= devise_error_messages!
%fieldset
.clearfix= f.input :name, :required => true
.clearfix= f.input :email, :required => true
.clearfix= f.input :subdomain, :required => true
.clearfix= f.input :password, :required => true, :input_html => {:minlength => 6}
.clearfix= f.input :password_confirmation, :input_html => {:minlength => 6}
.form-actions
= f.submit t('labels.signup'), :class => 'btn btn-success'
%p= render "links"
それが役に立てば幸い..