Rails 添加 Twilio 子帐户:未初始化的常量 User::Twilio [英] Rails Adding Twilio Sub Accounts : Uninitialized constant User::Twilio
问题描述
*********更新:我刚刚尝试重新启动 Rails 服务器,似乎成功了!
*********UPDATE : I just tried restarting the Rails server, and it seemed to have worked!
我已经按照 Michael Hartl 的 Rails 教程构建了一个基本的身份验证系统,现在我想做的是在用户注册时使用 Twilio 的 API 创建一个 Twilio 子帐户.
I've built a basic authentication system following Michael Hartl's tutorial on Rails, and now what I would like to do is to use Twilio's API to create a Twilio Sub Account when a user registers.
https://www.twilio.com/docs/api/rest/subaccounts
我对如何创建它的想法是在用户模型中使用 before_save,并让 twilio 为子帐户创建身份验证令牌和帐户 Sid.问题是,当我点击提交时,我得到 -
My thoughts on how to create it were to use a before_save in the User Model, and have twilio create the Auth Token and Account Sid for the sub account. The problem is, that when I hit submit, I get --
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'
这是我当前的用户模型:
Here's my Current User Model :
#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 的工作方式.如果我在做一些完全古怪的事情,请告诉我!
Any help on what I should do inside create_twilio_subaccount would be greatly appreciated it. This is just my guess at how to do it, based on how the remember_token worked. Let me know if I'm doing something completely wacky!
推荐答案
我刚刚尝试重新启动 Rails 服务器,似乎成功了!
I just tried restarting the Rails server, and it seemed to have worked!
这篇关于Rails 添加 Twilio 子帐户:未初始化的常量 User::Twilio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!