0

这是我正在生成的响应:

Processing by OrdersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"C/1Fmc4iEAiRGXwTcQNcDdX3j81KyhMHnfNcuYC/p1Y=", "order"=>{"video_id"=>"99", "member_id"=>"36", "price"=>"1000", "stripe_card_token"=>"tok_2mYQqbzBgt1EYy"}}
Completed 404 Not Found in 22ms

ActionController::RoutingError - No route matches {:action=>"show", :controller=>"orders", :id=>#<Order id: nil, video_id: 99, member_id: 36, stripe_card_token: nil, price: 1000, created_at: nil, updated_at: nil>}:

基本上,在创建新订单时会生成所有正确的属性,但由于某种原因,stripe_card_token 没有被传递给参数。

这是我的订单模型:

  attr_accessible :price, :stripe_card_token, :member_id, :video_id

  belongs_to :video
  belongs_to :member

  accepts_nested_attributes_for :video
    accepts_nested_attributes_for :member

    validates :member_id, presence: true
    validates :video_id, presence: true
    validates :price, presence: true

  attr_accessor :stripe_card_token


   def save_with_stripe
    if valid?
      @charge = Stripe::Charge.create(
        amount: price,
        currency: "gbp",
        card: stripe_card_token,
        description: "something"
      )

      save!
    end
  rescue
    errors.add :base, "There was a problem with Stripe"
  end

这是我的订单控制器:

  def new
    @order = Order.new(:member_id => @member.id, :video_id => @video.id, :price => @video.price)    
  end

  def create
    @order = Order.new(params[:order])
    if @order.save_with_stripe
      flash[:success] = "Enjoy the video!"
      redirect_to order_path(@order)
    else
      render partial: 'orders/buynow'
    end
  end

  def show
    @order = Order.find(params[:id])
  end

我的表格:

<%= form_for @order do |f| %>
    <% if @order.errors.any? %>
      <div class="error_messages">
        <h2><%= pluralize(@order.errors.count, "error") %> stopped your card from being charged:</h2>
        <ul>
        <% @order.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

    <%= f.hidden_field :video_id %>
    <%= f.hidden_field :member_id %>
    <%= f.hidden_field :price %>
    <%= f.hidden_field :stripe_card_token %>

    <% if current_user.stripe_customer_token.present? %>
      Credit card has been provided.
    <% else %>
      <div class="field">
        <%= label_tag :name, "Name on card" %>
        <%= text_field_tag :name, nil, name: nil %>
      </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>
    <% end %>
    <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 class="actions">
      <%= f.submit "Buy Now" %>
    </div>
  <% end %>

还有我的咖啡脚本:

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

order =
  setupForm: ->
    $('#new_order').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        order.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, order.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#order_stripe_card_token').val(response.id)
      $('#new_order')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

你能帮我理解我出了什么问题吗?

编辑1:

这是我的开发日志中生成的新响应,现在我解决了尝试将未定义的“电子邮件”局部变量传递到“描述”键的问题。

Started POST "/orders" for 127.0.0.1 at 2013-10-19 14:45:22 +0100
Processing by OrdersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"C/1Fmc4iEAiRGXwTcQNcDdX3j81KyhMHnfNcuYC/p1Y=", "order"=>{"video_id"=>"99", "member_id"=>"36", "price"=>"1000", "stripe_card_token"=>"tok_2mZBErSMVjAa3F"}}
   (1.9ms)  BEGIN
  SQL (2.4ms)  INSERT INTO "orders" ("created_at", "member_id", "price", "stripe_card_token", "updated_at", "video_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["created_at", Sat, 19 Oct 2013 13:45:24 UTC +00:00], ["member_id", 36], ["price", 1000], ["stripe_card_token", nil], ["updated_at", Sat, 19 Oct 2013 13:45:24 UTC +00:00], ["video_id", 99]]
   (2.3ms)  COMMIT
Redirected to http://eqrfqer.xavy.dev/orders/13

由于某种原因,在 params 中生成的 stripe_card_token 没有被插入到我的 Order 模型中。这就是问题,但我不明白为什么?

编辑2:

我想也许问题实际上是我试图将未定义的电子邮件传递给“描述”。自从我改变了这一点,我可以看到费用已转入我的 Stripe 帐户。我不明白为什么它在日志中看起来好像 stripe_card_token 是“nil”,但我认为它一定是一个安全功能?

无论如何,它现在似乎正在工作。

4

1 回答 1

0

通过写这行:

attr_accessor :stripe_card_token

您正在杀死用于设置的 rails 功能stripe_card_token

如果它是您数据库中的一列,请将其删除!

于 2013-10-19T15:34:41.337 回答