1

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?

YUTAKA
  • 21
  • 5
  • You're not sending an array, you're sending a single string in `item_ids`; that's where your problem lies. You might find the answer here: https://stackoverflow.com/questions/4508941/passing-an-array-through-a-hidden-field-tag-in-rails – Cassandra S. Jan 12 '21 at 06:58

1 Answers1

0

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

Tio Lampard
  • 1
  • 2
  • 4