1

导轨 5.2.4.4 红宝石 2.5.1p57

我想做的事: 通过 Stripe 构建结账服务

错误: rails NoMethodError(“1”的未定义方法`map':String您是说吗?点击):

显示.html.erb

<% if @items.any? %>
    <% @items.each do |item| %>
      <ul class="row-col-1 border border-dark d-flex justify-content-between px-5 py-2 mb-3">
        <li class="collection-item">
          <%= item.name %>
        </li>
        <li class="collection-item ">
          <%= item.price%>
          <%= link_to "delete", item_delete_in_baskets_path(item), method: :post, data: { confirm: "You sure?" }, class:"ml-2"%>
        </li>
      </ul>
    <% end %>
    <div class="basket_total_amount float-right">
      <%= "Total price::  CAD $ #{@total_price}" %>
    </div>
    <%= form_tag charge_path do %>
      <% @items.each do |p|%>

        <%= hidden_field_tag 'item_ids[]', p.id %>

      <% end %>
      <script src="https://checkout.stripe.com/checkout.js" 
              class="stripe-button" 
              data-key="<%= ENV['STRIPE_API_KEY']%>" 
              date-description="payment"
              data-amount="<%= @total_price %>"
              data-locale="auto"
              data-currency="cad">
      </script>
    <% end %>
<% else %>
  <h3 class="text-center" id="basket_nop">...you have nothing on your basket</h3>
<% end %>
</div

收费控制器

class ChargesController < ApplicationController
  def create
    Stripe.api_key = ENV['STRIPE_SECRET_KEY']
    token = params[:stripeToken]
    item_ids = params[:item_ids].map(&:to_i)

    items = current_user.basket.items.where(id: item_ids)
    total = items.sum[:price]

    Stripe::Charge.create({
      amount: total,
      currency: 'cdn',
      description: 'Example charge',
      source: token
    })
    redirect_to root_path, success: 'success your payment'
  end
end

我的终端

Parameters: {"utf8"=>"✓&quot;, "authenticity_token"=>"BIs...", "item_ids"=>"1", "stripeToken"=>"tok...", "stripeTokenType"=>"card", "stripeEmail"=>"my email"}NoMethodError (undefined method `map' for "1":String
Did you mean?  tap):

任何人都可以帮我解决这个问题吗?

4

1 回答 1

0

map 只处理数组数据, params[:item_ids] 的内容是字符串,所以只需使用params[:item_ids].to_i

于 2021-01-12T06:46:40.447 回答