简体   繁体   English

参数未传递任何东西或传递null

[英]Parameters are not passing anything or passing null

Payments Controller: 付款控制器:

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: _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: Show.html.erb:

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

I expected the <%= hidden_field_tag(:product_id, @product.id)%> to pass the product id to the payment controller at @product = Product.find(params[:product_id]) . 我期望<%= hidden_​​field_tag(:product_id,@ product.id)%>将产品ID传递给@product = Product.find(params[:product_id])处的付款控制器。 But it does not seem to be working. 但是它似乎没有用。 Any help? 有什么帮助吗? An error I am getting from this is: ActiveRecord::RecordNotFound in PaymentsController#create Couldn't find Product without an ID 我从中得到的错误是:PaymentsController#create中的ActiveRecord :: RecordNotFound无法找到没有ID的产品

Okay the solution me and my mentor found was to change the value to a non dynamic value. 好的,我和我的导师发现的解决方案是将值更改为非动态值。 I did not understand why this worked but it did: 我不明白为什么这行得通,但确实行得通:

Thanks for everyone that helped on the way 谢谢大家的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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