2012-10-06 77 views
0

我能夠使用Ryan Bates的教程集成條紋。現在,我希望能夠允許用戶更新他們的信用卡信息。我在change_plan_path中設置了一個表單,它向用戶/ update_card發送一個PUT請求。然而,當我點擊更新信用卡按鈕,我得到的錯誤:模板丟失 - >對於PUT請求

Template is missing 

Missing template users/update_card, application/update_card with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "C:/Sites/dentist/app/views" 

下面是我用發送PUT請求的形式:

<%= form_tag("https://stackoverflow.com/users/update_card", :method => "put", :class => "edit_user", :id => "change_card") do %> 
    <%= hidden_field_tag :stripe_card_token %> 

    <div id="stripe_error" class="alert"> 
     <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript> 
    </div> 

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


    <%= submit_tag("Update My Credit Card", :class => "button") %> 

這是我的routes.rb文件:

put '/users/update_plan', to: 'users#update_plan' 
    put '/users/update_card', to: 'users#update_card' 
    resources :users 
    resources :sessions, only: [:new, :create, :destroy] 
    resources :phones, only: [:new, :create, :destroy] 
    resources :find_numbers, only: [:new, :create] 

    match '/signup', to: 'users#new' 
    match '/login', to: 'sessions#new' 
    match '/signout', to: 'sessions#destroy', via: :delete 
    match '/change_plan', to: 'users#change_plan' 
    match '/change_card', to: 'users#change_card' 

    root to: 'static_pages#home' 

    match '/product_demo', to: 'static_pages#product_demo' 

    match '/pricing', to: 'plans#index' 

    match '/contact', to: 'static_pages#contact' 

這裏是我正在使用的條紋Coffescript。它似乎還沒有工作,但也許它與它有關。 (開始changecard.setupForm())

jQuery -> 
    Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')) 
    subscription.setupForm() 

subscription = 
    setupForm: -> 
    $('#new_user').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 
     $('#user_stripe_card_token').val(response.id) 
     $('#new_user')[0].submit() 
    else 
     $('#stripe_error').text(response.error.message) 
     $('input[type=submit]').attr('disabled', false) 

changecard.setupForm() 

changecard = 
    setupForm: -> 
    $('#change_card').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 
     $('#user_stripe_card_token').val(response.id) 
     $('#change_card')[0].submit() 
    else 
     $('#stripe_error').text(response.error.message) 
     $('input[type=submit]').attr('disabled', false) 

回答

1

通常當你得到這個錯誤,控制器動作已經執行,它試圖呈現的東西回瀏覽器後,它的。除非您明確告訴它要呈現什麼內容,否則它會根據控制器/操作嘗試並提供視圖。不知道你的控制器是什麼樣子的,很難準確地說出信息更新後你會發生什麼。我希望你要麼重定向到一個新的頁面,要麼渲染一個頁面。

如果你想重定向,你可以使用redirect_to並指定路徑。在下面的例子中,我假設你想重定向到用戶show動作:

redirect_to user_url(current_user) 

如果你想呈現一些內容,你將需要創建update_card.html.erb HTML模板或呈現一些任意內容如:

render :text => "yay" 
+0

不錯!感謝您的幫助Beerlington - 我以不同的行動爲行動編寫了代碼......非常天真。 –

+0

這將做到這一點:) –