平衡付款 - 路由错误

时间:2013-05-01 02:42:53

标签: jquery ruby-on-rails balanced-payments

对于我的应用程序,我有用户。我正在尝试使用Balanced Payments来允许用户保存他们的信用卡信息以便以后使用。我已按照以下两个链接作为指南,目前有以下错误:

Routing Error
No route matches [POST] "/store_credit_card"

我遵循了GISTRails form submit handler not being executed

  • balanced_controller.rb - 已创建
  • _credit_card_form.html.haml - 已创建并置于app>view>balanced
  • credit_card_submission.js - 已创建并置于app>assets>javascript
  • 在我的用户模型中添加了一个字段:balanced_account_uri string
  • 替换了Balanced marketplace uri的.js文件中的占位符文本
  • 更新了IPv_controller.rb
  • 中方法的routes.rb

有人知道它为什么不起作用吗?

的routes.rb

resources :users do
    resources :balanced do
      member do
        post :store_credit_card, :create_balanced_account
      end
    end
end

查看/用户/ edit.html.erb

<div class = "container">

<script type="text/javascript" src="https://js.balancedpayments.com/v1/balanced.js"></script>

<script type="text/javascript">
    balanced.init('YOUR MARKETPLACE URI HERE');
</script>

<%= render 'balanced/credit_card_form' %>

</div>

查看/平衡/ _credit_card_form.html.haml

= form_tag "/store_credit_card", :method => :post, :id => "credit-card-form", :class => "form-horizontal" do
  .control-group
    = label_tag "cc-number", "Card Number", :class => "control-label"
    .controls
      = text_field_tag "cc-number", nil, :placeholder => "Enter Credit Card Number", :class => "cc-number", :autocomplete => "off"
  .control-group
    = label_tag "cc-em", "Expiration", :class => "control-label"
    .controls
      = text_field_tag "cc-em", nil, :placeholder => "Expiration Month", :class => "cc-em", :autocomplete => "off"
      = text_field_tag "cc-em", nil, :placeholder => "Expiration Year", :class => "cc-ey", :autocomplete => "off"
  .control-group
    = label_tag "cc-csc", "Security Code", :class => "control-label"
    .controls
      = text_field_tag "cc-csc", nil, :placeholder => "Security Code", :class => "cc-csc", :autocomplete => "off"
  .control-group
    .controls
      = submit_tag "Submit", :class => "btn btn-primary btn-large"

资产/ JavaScript的/ credit_card_submissions.js

var marketplaceUri = 'YOUR MARKETPLACE URI HERE';
var requestBinUrl = '/store_credit_card'


var debug = function(tag, content) {
  $('<' + tag + '>' + content + '</' + tag + '>').appendTo('#result');
};

try {
balanced.init(marketplaceUri);
} catch (e) {
debug('code', 'balanced.init error!');
}

function balancedCallback(response) {
var tag = (response.status < 300) ? 'pre' : 'code';
debug(tag, JSON.stringify(response));
switch(response.status) {
    case 201:
        console.log(response.data);
        var $form = $("#credit-card-form");
        var card_token_uri = response.data['uri'];
        $('<input>').attr({
            type: 'hidden',
            value: card_token_uri,
            name: 'balancedCreditCardURI'
        }).appendTo($form);
        $form.attr({action: requestBinUrl});
        $form.get(0).submit();
        break;
    case 400:
        console.log(response.error);
        break;
    case 404:
        console.log(response.error);
        break;
}
}

var tokenizeCreditCard = function(e) {
e.preventDefault();

var $form = $('#credit-card-form');
var creditCardData = {
    card_number: $form.find('.cc-number').val(),
    expiration_month: $form.find('.cc-em').val(),
    expiration_year: $form.find('.cc-ey').val(),
    security_code: $form.find('.cc-csc').val()
};

balanced.card.create(creditCardData, balancedCallback);
};

$(function(){
$('#credit-card-form').submit(tokenizeCreditCard);
});

balanced_controller.rb

class BalancedController < ApplicationController
  def create_balanced_account
    current_user.balanced_account_uri = Balanced::Marketplace.my_marketplace.create_account(:email => current_user.email, :type => 'person').uri
    current_user.save
    # add a redirect to the desired path
    redirect_to root_url
  end

  def store_credit_card
    balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
    balanced_account.add_card(params[:balancedCreditCardURI])
    # add a redirect to the desired path
    redirect_to root_url
  end

  def store_bank_account
    balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
    balanced_account.add_card(params[:balancedBankAccountURI])
    # add a redirect to the desired path
    redirect_to root_url
  end

  def process_payment
    balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
    account.debit(
      :amount => 1000, # or params[:amount]
      :description => "Balanced Payments transaction",
      :appears_on_statement_as => "Balanced Payments")
    # add a redirect to the desired path
    redirect_to root_url
  end
end

1 个答案:

答案 0 :(得分:0)

根据您的路线,您的帖子路径不会是"/store_credit_card",而是"users/:user_id/balanced/:balanced_id/store_credit_card"

因此,您需要更改form_tag "/store_credit_card"以匹配该内容。

运行rake routes以查看当前定义的路线。

此外,您可以使用路由生成的网址助手而不是form_tag中的字符串:

form_tag users_balanced_store_credit_card_path(@user, @balanced)

上述内容可能有误,rake routes会告诉您确切的路径和帮助名称。