简体   繁体   中英

rails NoMethodError (undefined method `map' for “1”:String Did you mean? tap):

Rails 5.2.4.4 ruby 2.5.1p57

What I want to do: Build check out service though Stripe

error: rails NoMethodError (undefined method `map' for "1":String Did you mean? tap):

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

my terminal

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):

Anynone could you help me to solve this problem please?

map just working on array data, the content from params[:item_ids] is string, so just use params[:item_ids].to_i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM