我是Rails的新手,这是我第一次在我的应用中实现API。我正在创建一个允许用户使用twilio安排短信(SMS)的应用。我试图传递号码'这样用户就可以输入号码了。当我对一个号码进行硬编码时,我已经让该应用程序正常工作但我得到了一个" A' To'电话号码是必需的。"当我试图传递一个论点。
这是控制器
class TextMessagesController < ApplicationController
def index
@message = TextMessage.all
end
def new
@message = TextMessage.new
end
def create
@active = 'messages'
@message = TextMessage.send_sms(params[:number], params[:message])
if @message.save
flash[:success] = "Text Message Sent."
redirect_to message_path
else
render 'home'
end
end
def text_message_params
params.require(:text_messages).permit(:number, :message)
end
end
这是模特
class TextMessage < ActiveRecord::Base
belongs_to :user
validates :message, length: { maximum: 160 }, presence: true
def self.send_sms(number, message)
twilio_account_sid = 'XXXXXX'
twilio_auth_token = 'XXXXXX'
twilio_number = 'XXXXXX'
@client = Twilio::REST::Client.new twilio_account_sid, twilio_auth_token
message = @client.account.sms.messages.create(
:from => "+1#{twilio_number}",
:to => "#{number}",
:body => "#{message}"
)
end
端
任何人都知道我犯了哪些错误?
答案 0 :(得分:1)
我的猜测是params[:number]
是空白的:
@message = TextMessage.send_sms(params[:number], params[:message])
也许这应该是params[:text_messages][:number]
而已?基于你的text_message_params
方法(你没有利用它)。
@message = TextMessage.send_sms(params[:text_messages][:number], params[:text_messages][:message])
答案 1 :(得分:0)
class TextMessagesController < ApplicationController
def index
@message = TextMessage.all
end
def new
@message = TextMessage.new
end
def create
@active = 'messages'
@message = TextMessage.send_sms(text_message_params[:number], text_message_params[:message])
if @message
flash[:success] = "Text Message Sent."
redirect_to text_messages_path
else
render 'home'
end
end
def text_message_params
params.require(:text_message).permit(:number, :message)
end
end