Rails Adding Twilio Sub Accounts : Uninitialized constant User::Twilio
-
12-12-2019 - |
質問
*********UPDATE : I just tried restarting the Rails server, and it seemed to have worked!
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
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
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!
解決
I just tried restarting the Rails server, and it seemed to have worked!