简体   繁体   English

Rails Marketplace应用中的条纹撤回操作

[英]Stripe withdrawal action in rails marketplace app

I'm new to rails. 我是新手。 I'm trying to create a rails marketplace where sellers can cash out their acquired funds from sales on the site. 我正在尝试创建一个Rails市场,卖家可以在该市场上从网站上的销售中兑现所获得的资金。

I'm confused how to configure my withdrawal form and my orders controller. 我很困惑如何配置我的提款单和订单控制器。

When I simply just go to localhost:3000/withdrawal, a Stripe Recipient with just name is created on my Stripe dashboard without even completing the form. 当我只是简单地转到localhost:3000 / withdrawal时,即使在未填写表单的情况下,在我的Stripe仪表板上也会创建一个仅带有名称的Stripe收件人。 My form is nonexistent because everything I've tried for form_for generates an error. 我的表单不存在,因为我为form_for尝试的所有操作都会产生错误。

I want the user to input their info and then choose to submit it, not create the recipient when "cash out" (which leads to the withdrawal path) is clicked. 我希望用户输入他们的信息,然后选择提交它,而不是在单击“兑现”(这导致提款路径)时不创建收件人。

The stripe documentation is helpful, but I'm not sure how to create my form. 条纹文档很有帮助,但是我不确定如何创建表单。

Here is my withdrawal action in my orders controller. 这是我的订单控制器中的提款操作。 I'm wondering if I need a new action within withdrawal? 我想知道是否需要在提款中采取新措施? But not sure if that's possible? 但是不确定是否可行?

 def withdrawal
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]

recipient = Stripe::Recipient.create(
  :name => current_user.full_name,
  :type => "individual",
  :bank_account => token
  )

transfer = Stripe::Transfer.create(
  :amount => (@funds).floor,
  :currency => "usd",
  :recipient => @seller.recipient
  )
  end

And my withdrawal.html.erb. 还有我的drawing.html.erb。 I know I am missing a form tag and submit, but everything I've tried just processes an error. 我知道我缺少表单标记并提交,但是我尝试过的所有操作都只会处理错误。 I don't know what form_for to call. 我不知道要用什么form_for。 I've tried "order" but it results in an error. 我已经尝试过“订购”,但是会导致错误。

   <div class="text-center">
       <h1>Bank Account Information</h1>


      <div class="form-group">
       <%= label_tag :name %>
       <%= text_field_tag :name, nil, { :name => nil, :'data-stripe' => "name", class: "form-control" } %>
       </div>

        <div class="form-group">
         <%= label_tag :withdrawal_amount %>
         <%= text_field_tag :withdrawal_amount, nil, { :name => nil, :'data-stripe' => "amount", class: "form-control" } %>
  </div>
       <div class="form-group">
              <%= label_tag :routing_number %>
              <%= text_field_tag :routing_number, nil, { :name => nil, :'data-stripe' => "routingNumber", class: "form-control" } %>
       </div>
       <div class="form-group">
         <%= label_tag :account_number %>
         <%= text_field_tag :account_number, nil, { :name => nil, :'data-stripe' =>    "accountNumber", class: "form-control" } %>
       </div>

I'd appreciate any guidance on how to create this "cash out" action. 感谢您提供有关如何创建此“兑现”操作的指南。 Thanks. 谢谢。

form_for is for model objects. form_for用于模型对象。 If you're not using a model object, or don't want/need Rails to infer things from the model object, don't use form_for . 如果您不使用模型对象,或者不想/不需要Rails从模型对象推断事物,请不要使用form_for

For simple forms, instead use one of: 对于简单形式,请使用以下之一:

  • form_tag - Same syntax as form_for but without the model-related magic. form_tagform_for相同的语法,但没有与模型相关的魔术。
  • A plain old <FORM> tag. 一个普通的<FORM>标记。 ERB templates are, after all, just HTML with Ruby mixed in. 毕竟,ERB模板只是混合了Ruby的HTML。

Your view should look something like the following: 您的视图应类似于以下内容:

<%= form_tag withdrawl_path, method: :post do %>
  <input type="text" name="card[number]" />
  <!-- whatever fields you need for form -->
<% end %>

You will be able to access the submitted params via params[:card][:param_name] or use strong params and only permit params you need. 您将可以通过params [:card] [:param_name]访问已提交的参数,也可以使用强参数,并且只允许您需要的参数。

Use custom routes 使用自定义路线

get "/form" => "controller_name#action_for_form", as: :action_for_form
post "/withdrawl" => "controller_name#withdrawl", as: :withdrawl

Controller: 控制器:

def action_for_form
    # whatever code you need to setup form
    @seller = Seller.find(params[:seller_id])
end

def withdrawl
    # withdrawl code here
end

private
def card_params
  params[:card].permit(:card_token, :other_params)
end

Its worth taking the time to understand the form tag and using Rails outside of the Railsy way of form submissions. 花时间了解表单标签并在Railsy表单提交方式之外使用Rails是值得的。 It can get messy, but it allows for much more flexibility. 它可能会变得凌乱,但可以提供更大的灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM