参数未传递任何内容或未传递空

时间:2019-01-15 10:15:40

标签: javascript html ruby-on-rails ruby heroku

付款控制器:

class PaymentsController < ApplicationController
  before_action :authenticate_user!
  def show
    @user = current_user
    @orders = Order.where(user_id: @user.email)
    @products = Product.all
  end

  def create
    @product = Product.find(params[:product_id])
    token = params[:stripeToken]
    # Create the charge on Stripe's servers - this will charge the user's card
    price = @product.avgprice.to_i*100
    @user = current_user
    begin
      charge = Stripe::Charge.create(
        amount: price,
        currency: "gbp",
        source: token,
        description: params[:stripeEmail],
        reciept_email: @user.email
      )

      if charge.paid
        Order.create(product_id: @product.id, user_id: @user.email, total: price)
      end
    rescue Stripe::CardError => e
      #The card has been declined
      body = e.json_body
      err = body[:error]
      flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]}"
    end
    redirect_to(payments_show_path)
  end
end

_stripe_checkout_button.html:

<%price = @product.avgprice.to_i * 100%>
<form action="your-server-side-code" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="<%= Rails.configuration.stripe[:publishable_key] %>"

    data-amount="<%=price%>"
    data-name= "<%=@product.name%>"
    data-description="<%=@product.description%>"
    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
    data-locale="auto"
    data-currency="gbp">

  </script>
  <%= hidden_field_tag(:product_id, @product.id)%>
</form>

Show.html.erb:

<%= form_with(url: '/payments/create') do |form| %>
<%= render partial: 'shared/stripe_checkout_button'%>
<% end %>

我希望<%= hidden_​​field_tag(:product_id,@ product.id)%>将产品ID传递给@product = Product.find(params[:product_id])处的付款控制器。但是它似乎没有用。有什么帮助吗?我从中得到的错误是: PaymentsController#create中的ActiveRecord :: RecordNotFound 没有ID的产品找不到

1 个答案:

答案 0 :(得分:0)

好的,我和我的导师发现的解决方案是将值更改为非动态值。我不明白为什么这行得通,但行得通:

感谢所有在此过程中提供帮助的人