我使用Twilio API构建了一个带有电话文本用户界面的志愿者跟踪应用程序。我不需要视图,因此我的控制器包含以下代码:
class TwilioController < ApplicationController
include TwilioHelper
def sms_receive
user = User.find_or_create_by(phone_number: params[:From])
text = Text.create(user_id: user.id, body: params[:Body].capitalize, date: DateTime.now)
activity_log = ActivityLog.new(user_id: user.id, phone_number: "xxx-xxx-#{user.last_four_digits}", text_id: text.id)
args = {user: user, text: text, activity_log: activity_log, options: params}
volunteer_manager = VolunteerHandler.new(args)
replies = volunteer_manager.process
replies.each {|reply| text_response(reply, args[:options])}
end
def text_response(reply, args)
account_sid = ENV['ACCOUNT_SID']
auth_token = ENV['AUTH_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
client.account.messages.create(:body => reply, :to => args[:From], :from => args[:To])
render nothing: true and return
end
end
用户将发送多命令字符串(即“In with American Red Cross”)。在这种情况下,两个命令将执行'In'和'with American Red Cross'。这些命令会返回一系列字符串,例如['感谢您参与志愿服务','您希望美国红十字会与您保持联系以获取未来的志愿服务机会吗?']。此数组是 回复 指向的本地变量。
如果我取消渲染没有:true并返回代码然后我得到错误:ActionView :: MissingTemplate缺少模板twilio / sms_receive 我可以创建不必要的视图并解决我的问题,但这似乎不是最好的解决方案。
非常感谢任何和所有帮助。谢谢。
答案 0 :(得分:2)
由于replies
是一个多次迭代text_response
执行render nothing: true and return
的数组,这是您面临的错误原因。
尝试从循环中获取render
语句。
class TwilioController < ApplicationController
include TwilioHelper
def sms_receive
user = User.find_or_create_by(phone_number: params[:From])
text = Text.create(user_id: user.id, body: params[:Body].capitalize, date: DateTime.now)
activity_log = ActivityLog.new(user_id: user.id, phone_number: "xxx-xxx-#{user.last_four_digits}", text_id: text.id)
args = {user: user, text: text, activity_log: activity_log, options: params}
volunteer_manager = VolunteerHandler.new(args)
replies = volunteer_manager.process
replies.each {|reply| text_response(reply, args[:options])}
render nothing: true and return
end
def text_response(reply, args)
account_sid = ENV['ACCOUNT_SID']
auth_token = ENV['AUTH_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
client.account.messages.create(:body => reply, :to => args[:From], :from => args[:To])
end
end