【问题标题】:JSON missing a template for this request format and variant coming from React using StripeJSON 缺少此请求格式的模板和来自 React 使用 Stripe 的变体
【发布时间】:2018-02-11 05:07:25
【问题描述】:

我正在使用 Rails 中的后端在 React 中开发在线商店,并且我使用 Stripe。在我向我的费用控制器发送获取请求成功发送后,我可以看到参数,一切都很好。

Started POST "/charges" for 127.0.0.1 at 2018-02-10 20:22:16 +0000
Processing by ChargesController#create as JSON
  Parameters: {"description"=>"Only the Book", "source"=>"tok_1Bu4rRHmkRoa1PQ1R3ndnG2o", "amount"=>100, "charge"=>{"description"=>"Only the Book", "source"=>"tok_1Bu4rRHmkRoa1PQ1R3ndnG2o", "amount"=>100}}
Redirected to http://localhost:3000/charges/new
Completed 302 Found in 2554ms (ActiveRecord: 0.0ms)

但是当控制器创建动作执行重定向到新方法时,我得到这个错误:

ActionController::UnknownFormat (ChargesController#new is missing a template for this request format and variant.

request.formats: ["application/json"]
request.variant: []):

我在新方法的控制器中尝试了 renspond_to :json,但不起作用,我尝试反序列化将参数传递给实例变量的参数,但它们无法工作,因为它们是 nil,我还没有尝试 json builder,因为我这样做了不知道这是否是更简单的方法。 这是我下面的代码: 收费控制器.rb 类 ChargesController

  def new
   @amount = params[:stripeAmount]
   @description = params[:stripeDescription]
  end

  def create

    customer = Stripe::Customer.create(
      :email => params[:stripeEmail],
      :source  => params[:stripeToken]
    )

    charge = Stripe::Charge.create(
      :customer    => customer.id,
      :amount      => params[:stripeAmount],
      :description => params[:stripeDescription],
      :currency    => 'GBP'
    )  
    rescue Stripe::CardError => e
      flash[:error] = e.message
      redirect_to new_charge_path
  end
end

这是我的 new.html.erb

<%= form_tag charges_path do %>
  <article>
    <% if flash[:error].present? %>
      <div id="error_explanation">
        <p><%= flash[:error] %></p>
      </div>
    <% end %>
    <label class="amount">
      <span>Amount: <%= (@amount) %></span>
    </label>
  </article>

  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description="<%= @description %>"
          data-amount="<%= @amount %>"
          data-locale="auto"></script>
<% end %>

非常感谢您的热心帮助!

【问题讨论】:

    标签: ruby-on-rails json reactjs request format


    【解决方案1】:

    看起来您没有使用 json 响应。你的救援是一个 html 响应,所以这绝对行不通。您需要将响应以 json 格式发回,因为这是您的 react 应用程序所期望的。自从我使用条纹以来已经有一段时间了,但是像这样:

    以下假设您拥有respond_with:

    respond_to :json
    
    def create
      # ... your stripe customer and charge api requests
      respond_with charge
    end
    

    或者如果你没有respond_with:

    def create
      # ... your stripe customer and charge 
      respond_to do |format|
        format.json { render json: charge }
      end
    end
    

    这是类似的问题:Rails: Return JSON of Stripe Response from Server

    【讨论】:

    • 非常感谢您的快速帮助我使用了第二种方法,但我也将它放在我的“新”方法中,因为我需要这些变量来填写表格中的信息是正确的方法它 ?现在它可以工作了,我得到了 200 的费用/新费用,但实际付款并未在条带 API 中进行。如果您有任何其他建议将不胜感激非常感谢您
    • 当您从 rails 控制台运行 Stripe::Charge.create 或之后调试 charge 对象时,您会得到什么响应?还需要知道您是否仍在测试,或者尝试在生产中运行它。我相信条纹有“测试模式”和“现场模式”。无论如何,这将是您在 SOF 上提出或搜索的单独问题。
    • 非常感谢,我最终发现我没有在条纹上传递正确的参数,因此根本没有创建充电对象,现在一切正常,非常感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多