2012-09-29 46 views
1

我喜欢Turbinehq如何让您为公司创建管理员帐户和子域名。测试一下,我发现在为公司创建子域名后,他们可以通过电子邮件邀请用户。使用邀请的用户自动成为同一公司的一部分。如何邀请新用户访问rails中的特定子域

我想在rails中模拟这个过程。我尝试了this初学者应用程序,但它不够严格。第一个问题我有一个如何设计下面的表格优惠:

  • 它是一个嵌套的资源 - 比如有一个accepts_nested_attributes_for :users模型Company ...?
  • 有没有更好的方法来设置?
  • 如果这确实是设置,那么如何预先为所有'admin'用户的被邀请者设置公司名称?
  • 有什么我想要做的流行指南?

TurbineHQ's account creation view

回答

1

我有同样的问题,前几天。我找到了一个可以正常工作的解决方案!

# 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" 

希望它可以帮助..