【发布时间】:2015-12-19 02:32:29
【问题描述】:
我有一个“工作网站”,用户可以在其中创建工作,其他用户可以申请这些工作。用户需要“代币”来创建工作,当他们没有剩余时被重定向到购买代币页面。令牌系统工作正常。 我已经设置了一个也可以使用的 Braintree 表单,但不会为用户创建任何令牌。我想在交易完成时为用户创建一个新令牌。
目前为止
routes.rb
resources :users do
resources :tokens
resources :transactions
令牌.rb
class Token < ActiveRecord::Base
belongs_to :user
end
交易.rb
class Transaction < ActiveRecord::Base
belongs_to :user
end
新的代币/支付方式
<form id="checkout" method="post" action="/transactions">
<div id="payment-form"></div>
<h2 class="mbs">Transaction</h2>
<p> Payment formfor one gig </p>
<%= form_tag transactions_path do%>
<p>Please enter your payment details:</p>
<div id="dropin"></div>
<%=submit_tag "Pay €1.99", class: "button mt1" %>
<%end%>
</form>
transactions_controller
class TransactionsController < ApplicationController
before_action :find_user
def new
end
def create
@result = Braintree::Transaction.sale(
amount: 10,
payment_method_nonce: params[:payment_method_nonce])
if @result.success?
@user = Token.build(params[:user_id => current_user.id])
redirect_to new_gig_path, notice: "Congraulations! Your transaction has been successfully!"
else
flash[:alert] = "Something went wrong while processing your transaction. Please try again!"
render :new
end
end
private
def generate_client_token
Braintree::ClientToken.generate
end
def token_params
params.require(:token).permit(:user_id)
end
def find_user
@user = User.find(params[:user_id])
end
end
但我收到错误消息“找不到具有 'id'=' 的用户
braintree 客户端令牌在令牌控制器中生成,因为表单位于新令牌页面上。这行得通。
这是我最后一次尝试这样做,但我尝试了多种为 current_user 创建令牌的方法,但我无法让它工作。 令牌模型属性为 token_id 和 user_id。
我们将不胜感激,谢谢。
【问题讨论】:
-
错误的堆栈跟踪是什么? exact 错误信息是什么?
-
对不起,我不知道堆栈跟踪错误是什么或如何检索它。我只用了几周的轨道。我得到的错误是:ActiveRecord::RecordNotFound in TransactionsController#create: Couldn't find User with 'id'= : and '@user = User.find(params[:user_id])' 被突出显示。
-
那么您似乎没有在参数中传递
user_id。修复它。也使用Token.create而不是Token.build -
Token.create(user_id: @user.id)或Token.create(user_id: current_user.id)(取决于哪个用户实际上应该在那里)
标签: ruby-on-rails post controller braintree