【问题标题】:How to display separate orders in an SMS using twilio & rails?如何使用 twilio & rails 在 SMS 中显示单独的订单?
【发布时间】: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


    【解决方案1】:

    你在那里看起来很好。我认为问题在于您的 Cart#meal_order 方法没有达到您的预期。我不确定to_a 在传递块时是否对块做任何事情。所以meal_order 只是返回cart_items,它在打印时输出cart_items 的属性。

    试试这样的:

    def meal_order
      item_names = cart_items.map do |item|
        "#{item.meal.name} - #{item.quantity}"
      end
      item_names.to_sentence
    end
    

    这样,我们使用 map 将购物车项目转换为包含名称和数量的字符串。由于您使用的是 Rails,因此您可以在数组上使用 to_sentence 将数组转换为逗号分隔的句子,最后两个元素用“and”分隔。

    让我知道这是否有帮助!

    【讨论】:

    • 你先生真棒!!这就是为什么在我破解 Rails 之前我应该​​专注于 Ruby。谢谢! :)
    • 没问题!祝应用程序的其余部分好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多