如何邀请新用户加入rails中的特定子域

时间:2012-09-29 23:07:35

标签: ruby-on-rails devise subdomain invite invitation

我喜欢Turbinehq如何让您一步到位地为公司创建管理员帐户和子域名。测试后,我发现在为公司创建子域后,他们可以通过电子邮件邀请用户。作为邀请的用户自动成为同一公司的一部分。

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

  • 这是一个嵌套资源吗? - 说Company模型有accepts_nested_attributes_for :users ......?
  • 有没有更好的方法来设置它?
  • 如果这确实是设置,那么如何为所有“管理员”用户的被邀请者预设公司名称?
  • 对于我正在尝试做的事情,是否有任何受欢迎的指南?

TurbineHQ's account creation view

1 个答案:

答案 0 :(得分: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"

希望有所帮助......