Rails添加Twilio子帐户:未初始化的常量User :: Twilio

时间:2012-10-02 06:37:41

标签: ruby-on-rails twilio

*********更新:我刚尝试重新启动Rails服务器,它似乎已经有效了!

我按照Michael Hartl关于Rails的教程构建了一个基本的身份验证系统,现在我想做的是在用户注册时使用Twilio的API创建一个Twilio子帐户。

https://www.twilio.com/docs/api/rest/subaccounts

我对如何创建它的想法是在用户模型中使用before_save,并让twilio为子帐户创建Auth Token和Account Sid。问题是,当我点击提交时,我得到 -

NameError in UsersController#create

uninitialized constant User::Twilio
Rails.root: C:/Sites/dentist

Application Trace | Framework Trace | Full Trace
app/models/user.rb:45:in `create_twilio_subaccount'
app/controllers/users_controller.rb:13:in `create'

这是我当前的用户模型:

#Twilio authentication credentials
ACCOUNT_SID = '####removed for stackoverflow#####'
ACCOUNT_TOKEN = '####removed for stackoverflow#####'


# == Schema Information
#
# Table name: users
#
#  id                 :integer          not null, primary key
#  name               :string(255)
#  email              :string(255)
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  password_digest    :string(255)
#  remember_token     :string(255)
#  twilio_account_sid :string(255)
#  twilio_auth_token  :string(255)
#

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token
  before_save :create_twilio_subaccount

  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: true

  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true


    private

      def create_remember_token
        self.remember_token = SecureRandom.urlsafe_base64
      end

    def create_twilio_subaccount     
      @client = Twilio::REST::Client.new(ACCOUNT_SID, ACCOUNT_TOKEN)
      @subaccount = @client.accounts.create({:FriendlyName => self[:email]})
      self.twilio_account_sid = @subaccount.sid
      self.twilio_auth_token  = @subaccount.auth_token
    end

end

对于我应该在create_twilio_subaccount中做什么的任何帮助都将非常感激。根据remember_token的工作原理,这只是我对如何操作的猜测。如果我正在做一些完全古怪的事情,请告诉我!

1 个答案:

答案 0 :(得分:0)

我刚尝试重新启动Rails服务器,它似乎已经运行了!