简体   繁体   中英

I am trying to use the twilio-ruby gem And I can get text messages to work from the Rails Console but In my controller I am a getting an error

NameError in UsersController#create uninitialized constant TwilioClient::Twilio Extracted source (around line #5):

attr_reader:client

def initialize @client = Twilio::REST::Client.new account_sid, auth_token end

In My users_controller.rb

 def create

@user = User.new(user_params)

respond_to do |format|
  if @user.save
    format.html { redirect_to user_url(@user), notice: "User was successfully created." }
    format.json { render :show, status: :created, location: @user }
    TwilioClient.new.send_text(@user, "This is sent from the controller")
  else
    format.html { render :new, status: :unprocessable_entity }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end

end end

My twilio_client.rb was copied from gorails and looks like this https://github.com/gorails-screencasts/send-sms-with-twilio/blob/master/app/services/twilio_client.rb

I answered my question what I did was

twilio=TwilioClient.new

twilio.send_text(@user, "This is sent from the controller")

This worked for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM