【发布时间】:2014-12-07 12:49:08
【问题描述】:
我有一个订餐网络应用程序,可将订餐订单直接发送给我的送货司机。我想显示订购的每顿饭的清单以及数量。我无法以 SMS 形式正确显示此内容。
在我的应用程序中,用户可以选择餐点,然后将餐点作为 cart_item 添加到购物车中。一旦用户为购物车付款,就会产生费用。
代码如下:
charges_controller.rb
def create
@charge = Charge.new(charge_params)
@amount = @cart.total_price
@phone = @charge.phone
@location = @charge.address
@meal_order = @cart.meal_order
begin
customer = Stripe::Customer.create(
:email => @charge.email,
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe Customer',
:currency => 'usd'
)
# TWILIO
send_text_message(@phone, @meal_order, @location, @amount)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
@cart.destroy
respond_to do |format|
if @charge.save
format.html { redirect_to root_path, notice: 'Charge was successfully created.' }
format.json { render :show, status: :created, location: @charge }
else
format.html { render :new }
format.json { render json: @charge.errors, status: :unprocessable_entity }
end
end
end
def send_text_message(customer_phone, meal_order, location, amount)
number_to_send_to = "xxx-xxx-xxx"
account_sid = ENV["account_sid"]
auth_token = ENV["auth_token"]
twilio_phone_number = ENV["twilio_phone_number"]
@client = Twilio::REST::Client.new account_sid, auth_token
@client.account.messages.create(
:from => "#{twilio_phone_number}",
:to => number_to_send_to,
:body => "New meal order!
Customer Phone number: #{customer_phone}.
Meal Order: #{meal_order}.
Location: #{location}
Total: $#{amount / 100}"
)
end
cart.rb
def meal_order
cart_items.to_a do |item|
item.meal.name
item.quantity
end
end
我收到的 SMS 响应示例如下所示:
新餐单!
客户电话号码:xxx-xxx-xxxx
餐单: CartItemid:26,meal_id:6,cart_id:16,created_at:“2014-12-06 12:03:11”,updated_at:“2013-12-06 12:04:11”,数量:2, CartItemid:27,meal_id:3,cart_id:16,created_at:“2014-12-06 12:03:11”,updated_at:“2013-12-06 12:04:11”,数量:3。 位置:加利福尼亚州圣地亚哥客户街 6200 号
总计:45 美元
我希望它能够整齐地打印出餐点的名称和数量,而不是像以前那样打印出来。有什么想法吗?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 sms twilio