【问题标题】:rails NoMethodError (undefined method `map' for "1":String Did you mean? tap):rails NoMethodError(“1”的未定义方法`map':String您是说吗?点击):
【发布时间】:2021-04-16 22:10:54
【问题描述】:

导轨 5.2.4.4 红宝石2.5.1p57

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

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

show.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

charges_controller

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"=>"✓", "authenticity_token"=>"BIs...", "item_ids"=>"1", "stripeToken"=>"tok...", "stripeTokenType"=>"card", "stripeEmail"=>"my email"}NoMethodError (undefined method `map' for "1":String
Did you mean?  tap):

谁能帮我解决这个问题?

【问题讨论】:

标签: ruby-on-rails stripe-payments


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    相关资源
    最近更新 更多