我终于找到了如何使用此tutorial
实施Stripes Monthly Billing目前,我的#new_subscription表单和编辑表单的提交按钮创建信用卡数据并将其发送给Stripe。
但由于某些原因,我在更新信用卡时遇到此错误
can't convert Symbol into String
app/controllers/subscriptions_controller.rb:38:in `update'
问题:这个错误是由coffeescript引起的,还是我的模型中的update_card方法导致了这个问题?
编辑表格
<%= form_for @subscription, :html => { :id => "update_card" } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :plan_id %>
<%= f.hidden_field :user_id %>
<%= f.hidden_field :stripe_card_token %>
<h4>Change Credit Card</h4>
<div class="field">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil %>
</div>
<div class="field">
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
</div>
<div class="field">
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
</div>
<%= f.submit "Change Credit Card", :class => "btn btn-primary" %>
<% end %>
JS.COFFEE
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
subscription.setupForm()
changecard.setupForm()
###That part is working great in the coffee script.
subscription =
setupForm: ->
$('#new_subscription').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
subscription.processCard()
false
else
true
processCard: ->
card =
{number: $('#card_number').val(),
cvc: $('#card_code').val(),
expMonth: $('#card_month').val(),
expYear: $('#card_year').val()}
Stripe.createToken(card, subscription.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 200
$('#subscription_stripe_card_token').val(response.id)
$('#new_subscription')[0].submit()
else
$('#stripe_error').text(response.error.message)
$('input[type=submit]').attr('disabled', false)
###This is the Coffee for my Edit Form..
changecard =
setupForm: ->
$('#update_card').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
changecard.processCard()
false
else
true
processCard: ->
card =
{number: $('#card_number').val(),
cvc: $('#card_code').val(),
expMonth: $('#card_month').val(),
expYear: $('#card_year').val()}
Stripe.createToken(card, changecard.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 200
$('#subscription_stripe_card_token').val(response.id)
$('#update_card')[0].submit()
else
$('#stripe_error').text(response.error.message)
$('input[type=submit]').attr('disabled', false)
MODEL
class Subscription < ActiveRecord::Base
attr_accessible :plan_id, :user_id, :email, :stripe_customer_token, :last_4_digits,
:card_token, :card_name, :exp_month, :exp_year, :stripe_card_token
attr_accessor :stripe_card_token
def update_card(attrs)
customer = Stripe::Customer.retrieve(stripe_customer_token)
customer.email = "myemail@gmail.com"
customer.description = "Subscription"
###This is being used for the credit card data
customer.card = attrs[:stripe_card_token]
customer.save
rescue Stripe::StripeError => e
logger.error "Stripe Error: " + e.message
errors.add :base, "#{e.message}."
false
end
CONTROLLER
class SubscriptionsController < ApplicationController
def update
@subscription = current_user.subscription
### I use the method update_card to update the customer's card
@subscription.update_card(params[:subscription])
if @subscription.update_card
redirect_to edit_subscription_path,
flash[:success] = 'Credit Card was Succesfully Updated.'
else
flash.alert = 'Unable to update card.'
render :edit
end
end
日志
Started PUT "/subscriptions/5" for 127.0.0.1 at 2014-01-30 16:23:20 -0800
Processing by SubscriptionsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ehyUD17CbH7MMOe98s+Kqkh+wGghntWkG4OpoqbnQaA=",
"subscription"=>{"plan_id"=>"1", "user_id"=>"1",
"stripe_card_token"=>"tok_103PJj26f8kAprmwvAtoZFRq"}, "id"=>"5"}
User Load (0.8ms) SELECT "users".* FROM "users"
WHERE "users"."remember_token" = 'xEeu1X6IK9gUAsna9b6Mow'
LIMIT 1
Subscription Load (0.5ms) SELECT "subscriptions".*
FROM "subscriptions" WHERE "subscriptions"."user_id" = 1 LIMIT 1
Redirected to
Completed 500 Internal Server Error in 1283ms
TypeError (can't convert Symbol into String):
app/controllers/subscriptions_controller.rb:38:in `update'
答案 0 :(得分:2)
您使用update_card
作为参数调用params
:
@subscription.update_card(params)
和update_card
期待:stripe_card_token
中的params
:
def update_card(params)
#...
customer.card = params[:stripe_card_token]
但没有params[:stripe_card_token]
,令牌位于params[:subscription][:stripe_card_token]
:
Parameters: {
...
"subscription"=>{ ... "stripe_card_token"=>"tok_103PCo26f8kAprmwHCPjZ6T5"},
}
params
结构也符合表单的结构:
<%= form_for @subscription, :html => { :id => "update_card" } do |f| %>
...
<%= f.hidden_field :stripe_card_token %>
form_for @subscription
会将表单设置为使用subscription[X]
用于字段X
,以便hidden_field
在HTML中具有name="subscription[stripe_card_token]"
并转换为{控制器中的{1}}。
也许你的意思是说:
params[:subscription][:stripe_card_token]
您还有@subscription.update_card(params[:subscription])
的迷路电话,没有任何参数。您的整个控制器方法应该看起来更像这样:
@subscription.update_card
答案 1 :(得分:1)
我认为这是一个错字,你写的是'subscription.processCard()'而不是'changecard.processCard()'
changecard =
setupForm: ->
$('#update_card').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
changecard.processCard()
false
else
true