【问题标题】:Ruby On Rails iterator .each block errorRuby On Rails 迭代器 .each 块错误
【发布时间】:2014-04-30 16:49:26
【问题描述】:

我有一个异常被此代码捕获,我无法查明错误。

  def paypal_content
    payment = {}
    payment[:intent] = "sale"
    payment[:payer] = { :payment_method => "paypal" }
    payment[:redirect_urls] = { :return_url => "http://localhost:3000/payment/execute", :cancel_url => "http://localhost:3000"}

    items = []
    index = 0
    @cart.items.each do |item|
       items[index] = {}
       items[index][:name] = item.title
       items[index][:sku] = item.artist
       items[index][:price] = item.price.to_s
       items[index][:quantity] = "1"
       items[index][:currency] = "CHF"
       index++      
    end  <--- this is line 109

    item_list = {}
    item_list[:items] = items

    transactions = []
    transactions[0] = {}
    transactions[0][:item_list] = item_list
    transactions[0][:amount] = { :total => @cart.total_price.to_s, :currency => "CHF"}
    transactions[0][:description] = "from Larraby Blaine Esquire"

    payment[:transactions] = transactions

    return payment
  end

错误是 home_controller.rb:109 语法错误,意外的keyword_end home_controller.rb:125: 语法错误,意外 $end,期待关键字_end

如果我删除每个块都很好,所以我想我在块上犯了一个错误,但是什么错误??????

【问题讨论】:

    标签: ruby-on-rails syntax-error block each


    【解决方案1】:

    Ruby不知道++,写+= 1

    当你写index++时,它认为第一个+是加法,第二个+是一元符号。你不能有一个没有后面没有东西的标志,所以它需要一个表达式,但会找到end

    【讨论】:

    • 非常感谢您的解决方案和解释。我快要疯了。我知道这是一件非常简单的事情,我现在很确定,多亏了你的解释,我再也不会犯那个++错误了
    猜你喜欢
    • 2017-02-10
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2014-04-17
    • 1970-01-01
    相关资源
    最近更新 更多