2014-01-30 56 views
1

我終於想通了如何使用這個tutorial更新磁條信用卡的CoffeeScript

目前我的提交按鈕兩個我#new_subscription形式和編輯表單創建和信用卡數據發送到條紋實現條紋每月結算。

但由於某些原因,我不斷收到這個錯誤,當我更新信用卡

can't convert Symbol into String 
app/controllers/subscriptions_controller.rb:38:in `update' 

問:這個錯誤是由於CoffeeScript的,或者是在我的模型我update_card方法導致此問題?

EDIT FORM

<%= 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 = "[email protected]" 
    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 

參考:Stripe Reference for Ruby

日誌

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

好像我們正在處理同一件事,因爲您的某些帖子與我的應用有關。你可以給我發一封電子郵件,或者我可以有你的,因爲我想私下討論條紋。謝謝 – pwz2000

回答

2

你」重新撥打update_cardparams作爲參數:

@subscription.update_card(params) 

update_cardparams期待:stripe_card_token

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 %> 

T他form_for @subscription將設置表格以使用subscription[X]作爲字段X,以便hidden_field將在HTML中具有name="subscription[stripe_card_token]",並且在控制器中轉換爲params[:subscription][:stripe_card_token]

也許你的意思是說:

@subscription.update_card(params[:subscription]) 

您也可以不帶任何參數來@subscription.update_card流浪電話。您的整個控制器方法應該看起來更像這樣:

def update 
    @subscription = current_user.subscription 
    if @subscription.update_card(params[:subscription]) 
    redirect_to edit_subscription_path, :success => 'Updated Card.' 
    else 
    flash.alert = 'Unable to update card.' 
    render :edit 
    end 
end 
+0

ic,那麼我的update_card方法現在會有customer.card = params [:subscription]了嗎?還是保持不變? –

+0

'update_card'裏面的'params'已經是控制器'params'的正確組塊。我推薦'def update_card(attrs)'來避免名字混淆。 –

+0

好的,我已經更新了上面的代碼。它終於起作用,它將信用卡數據傳遞給分條。但在我的最後,我得到這個錯誤。無法將符號轉換爲字符串 –

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 
+0

是的,我想到了,我實際上修復了它,但我得到了相同的結果。 :/ –

相關問題