Ruby on rails 条纹收费。不能';找不到带有';id'=

Ruby on rails 条纹收费。不能';找不到带有';id'=,ruby-on-rails,ruby,Ruby On Rails,Ruby,我对Rails很陌生。在我的项目中,我使用Stripe,我一直在尝试收费。但每次我尝试运行它时,都会出现这个错误。我花了两天时间阅读和搜索,但我不知道它有什么问题: ActiveRecord::RecordNotFound in ChargesController#create Couldn't find Product with 'id'= Extracted source (around line #39): def amount_to_be_charged

我对Rails很陌生。在我的项目中,我使用Stripe,我一直在尝试收费。但每次我尝试运行它时,都会出现这个错误。我花了两天时间阅读和搜索,但我不知道它有什么问题:

ActiveRecord::RecordNotFound in ChargesController#create
Couldn't find Product with 'id'=

Extracted source (around line #39):       

    def amount_to_be_charged
      @amount = Product.find(params[:id]).unit_price * 100
    end
调用的代码
app/views/products/index.html.erb

<%= link_to 'Buy', new_charge_path(id: x.id), class:"btn btn-primary"%>
<h1>Charges</h1>
<%= form_tag charges_path do %>
  <article>
    <% if flash[:error].present? %>
      <div id="error_explanation">
        <p><%= flash[:error] %></p>
      </div>
    <% end %>
    <label class="amount">
      <span>Amount: <%= formated_amount(@amount) %> </span>
    </label>
  </article>

  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description="<%= @description %>"
          data-amount="<%= @amount %>"
          data-email="<%= current_user.email %>"
          data-bitcoin="true"
          data-locale="auto"></script>
<% end %>
我的路线

Rails.application.routes.draw do
  get 'thanks', to: 'charges#thanks', as: 'thanks'

  resources :charges, only: [:new, :create]
  resources :sales
  resources :invoices
  resources :products
  resources :categories
  devise_for :users
root 'products#index'
end
更新

My
new.html.erb

<%= link_to 'Buy', new_charge_path(id: x.id), class:"btn btn-primary"%>
<h1>Charges</h1>
<%= form_tag charges_path do %>
  <article>
    <% if flash[:error].present? %>
      <div id="error_explanation">
        <p><%= flash[:error] %></p>
      </div>
    <% end %>
    <label class="amount">
      <span>Amount: <%= formated_amount(@amount) %> </span>
    </label>
  </article>

  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description="<%= @description %>"
          data-amount="<%= @amount %>"
          data-email="<%= current_user.email %>"
          data-bitcoin="true"
          data-locale="auto"></script>
<% end %>
费用

正如您所看到的,数量已正确入位

我的谢谢.html.erb

<h2>Thank you for your payment!</h2><br>
<p>
Your payment of <strong><%= formated_amount(@amount) %></strong> has been sent.
</p>
谢谢您的付款
您的付款已发送。


要进行调试,请查看日志以查看发送到控制器的参数散列。还要查看源代码以了解x.id等于什么。尝试以其他格式发送id:

<%= link_to 'Buy', new_charge_path, id: x.id, class:"btn btn-primary"%>

似乎
参数[:id]
为零,并且
在每个操作之前运行,并尝试通过获取产品来确定价格。很可能
重定向到感谢路径
缺少id参数,请尝试以下操作:
重定向到感谢路径(id:params[:id])


提示:我建议在这里使用更具描述性的命名,而不是简单的
id
use
product\u id

我认为问题出在重定向到感谢路径中,您在这里尝试的是重定向到索引操作,但是,您有一个before_action筛选器,一旦发生重定向,它将被执行要收费的金额,但是参数散列是空的,因为在重定向期间您没有传递任何值。请对此进行分类,如果您打算只在创建过程中运行金额说明过滤器,请按如下所示更改操作前的

before_filter :amount_to_be_charged, only: [:create]
before_filter :description, only: [:create]

您是否专门将
id
#new
操作传递到
#create

通过以下步骤:

  • 您有一个链接
    new\u charge\u path(id:x.id)
    ,其中
    x
    可能是一个“产品”
  • 点击
    charges#new
    ,此时
    id
    应如预期的那样位于
    params
  • 拼图中缺少的部分:您的
    new.html.erb
    看起来像什么,它是否将
    id
    字段的值设置为
    params[:id]

  • 我在新的操作方法中使用了Rails.logger.debug(#{@amount.inspect})和Rails.logger.debug(#{@description.inspect}”),并对它们进行了设置。我更新了问题。我添加了new.html.erb和Thanke.html。erb@Xenetrix:尝试将
    添加到
    新.html.erb
    表单中的某个位置(即
    表单标签下的
    块)。这将允许将
    id
    传递给
    #create
    。此外,一旦您证明此方法有效(如果有效!)-考虑是否更好地将它添加到<代码>数据> /Cord.PARAM上>代码> CuthOut.js<代码>,以便条目将它传递给您。查看CuffOut.js文档以获得更多的细节。这感觉更安全,因为有人篡改<代码> ID 更困难。例如,如果有人发现您的内部ID,则可以支付。一个便宜的产品,同时交换一个更昂贵的身份证。。。