我正在试图弄清楚如何将自定义条纹表单集成到准系统rails应用程序,但到目前为止它是一段艰难的时期。
我遇到了错误:param is missing or the value is empty: charges
这是我的收费控制器:
class ChargesController < ApplicationController
def new
@charge = Charge.new
end
def create
@charge = Charge.new
if @charge.charging_card(charges_params)
redirect_to root_path, :notice => "Contribution was recorded succesfully!"
else
redirect_to about_path, :notice => "Transaction was not able to be recorded"
end
end
def charges_params
params.require(:charges).permit(:stripe_card_token)
end
end
这是我的收费观点:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Stripe Getting Started Form</title>
<!-- The required Stripe lib -->
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
// This identifies your website in the createToken call below
Stripe.setPublishableKey('I put my own test key here');
var stripeResponseHandler = function(status, response) {
var $form = $('#payment-form');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
// token contains id, last4, and card type
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and re-submit
$form.get(0).submit();
}
};
jQuery(function($) {
$('#payment-form').submit(function(e) {
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
</script>
</head>
<%= form_for @charge do |f| %>
<div class="form-inputs">
<div class="row">
<div class="small-12 medium-6 large-2 columns">
<%= label_tag :card_number, "Credit Card Number:" %>
</div>
<div class="small-12 medium-6 large-10 columns">
<%= text_field_tag :card_number, nil, name: nil, :data => {:stripe => 'number'} %>
</div>
<div class="small-12 medium-6 large-2 columns">
<%= label_tag :card_code, "Security Code on Card (CVV):" %>
</div>
<div class="small-12 medium-6 large-10 columns end">
<%= text_field_tag :card_code, nil, name: nil, :data => {:stripe => 'cvc'} %>
</div>
<div class="small-12 medium-6 large-2 columns">
<%= label_tag :card_month, "Card Expiration:" %>
</div>
<div class="small-6 medium-3 columns">
<%= select_month nil, {name: nil, id: "card_month"}, :data => {:stripe => 'exp-month'} %>
</div>
<div class="small-6 medium-3 columns">
<%= select_year nil, {name: nil, id: "card_year"}, :data => {:stripe => 'exp-year'} %>
</div>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
</div>
<div class="row">
<div class="form-actions small-9 small-offset-2 columns">
<%= f.submit "Donate" %>
</div>
</div>
</div>
<% end %>
这是我的收费模式:
class Charge < ActiveRecord::Base
Stripe.api_key = "I put my key here"
attr_accessor :stripe_card_token
def charging_card(charge)
begin
charge = Stripe::Charge.create(
:amount => 325, # amount in cents, again
:currency => "usd",
:card => stripe_card_token,
:description => "payinguser@example.com"
)
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
end
end
end
这是我的架构表:
ActiveRecord::Schema.define(version: 20140723082502) do
create_table "charges", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "stripe_card_token"
t.decimal "amount"
end
end
这是我的服务器日志:
Started POST "/charges" for 127.0.0.1 at 2014-07-23 01:57:45 -0700
Processing by ChargesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4FJAw5RTi1K2n3mC0lccoJ2iaDeQxfXRMGqi2kojsbk=", "date"=>{"month"=>"1",
"year"=>"2015"}, "commit"=>"Donate"}
Completed 400 Bad Request in 4ms
ActionController::ParameterMissing (param is missing or the value is empty: charge):
app/controllers/charges_controller.rb:17:in `charges_params'
app/controllers/charges_controller.rb:9:in `create'
Rendered c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.1/lib/action_dispatch/middleware/template
s/rescues/_source.erb (71.0ms)
Rendered c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.1/lib/action_dispatch/middleware/template
s/rescues/_trace.html.erb (4.0ms)
Rendered c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.1/lib/action_dispatch/middleware/template
s/rescues/_request_and_response.html.erb (3.0ms)
Rendered c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.1/lib/action_dispatch/middleware/template
s/rescues/diagnostics.erb within rescues/layout (221.1ms)
我想我不明白为什么我的强参数中的费用是空的?
您可以从我的代码中发现的任何其他建议或建议可以帮助我完成此项目,我们将不胜感激!
答案 0 :(得分:0)
尝试将强参数更改为以下内容:
def charges_params
if params[:charges] && params[:charges][:stripe_card_token].present?
params.require(:charges).permit(:stripe_card_token)
end
end