0

*********更新:我刚刚尝试重新启动 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 的工作方式。让我知道我是否在做一些完全古怪的事情!

4

1 回答 1

0

我刚刚尝试重新启动 Rails 服务器,它似乎成功了!

于 2012-10-02T15:04:02.053 回答