2017-07-16 5 views
0

私はStripe支払いフォームを生成するためのスクリプトを含むRailsフォームを持っています。フォーム内にはfields_forタグがネストされています。Railsネストフォームからのアクセスパラメータ

<div class="row"> 

    <div class="col-md-2"></div> 


     <div class="col-md-8"> 

     <h3>Confirm address and make payment</h3> 
     <br> 

     <%= form_tag charges_path do %> 

     <%= fields_for :charges do |f| %> 

      <div class="field"> 
      <%= f.label :first_name %> 
      <%= f.text_field :first_name, size: 20, :value => current_user.first_name, :class => 'form-control' %> 
      </div> 

      <div class="field"> 
      <%= f.label :last_name %> 
      <%= f.text_field :last_name, size: 20, :value => current_user.last_name, :class => 'form-control' %> 
      </div> 

      <div class="field"> 
      <%= f.label :address %> 
      <%= f.text_area :address, size: 40, :value => current_user.address, :class => 'form-control' %> 
      </div> 

      <div class="field"> 
      <%= f.label :city %> 
      <%= f.text_field :city, size: 20, :value => current_user.city, :class => 'form-control' %> 
      </div> 

      <div class="field"> 
      <%= f.label :state %> 
      <%= f.text_field :state, size: 2, :value => current_user.state, :class => 'form-control' %> 
      </div> 

      <div class="field"> 
      <%= f.label :email %> 
      <%= f.text_field :email, size: 40, :value => current_user.email, :class => 'form-control' %> 
      </div> 

      <%= f.hidden_field :total_price, :value => @cart.total_price %> 

      <% end %> 

     <br> 

      <article> 
       <% if flash[:error].present? %> 
       <div id="error_explanation"> 
        <p><%= flash[:error] %></p> 
       </div> 
       <% end %> 
       <label class="amount"> 
       <span>Amount: <%= number_to_currency(@cart.total_price * 7.35) %></span> 
       </label> 
      </article> 

      <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
        data-key="<%= Rails.configuration.stripe[:publishable_key] %>" 
        data-description="A one-time payment" 
        data-amount="<%= @cart.total_price * 735 %>" 
        data-locale="auto"></script> 
     <% end %> 



     </div> 

     <div class="col-md-2"></div> 



    </div> 

そして、ここに私の料金コントローラです:ここに私の全体の費用/ new.html.erbファイルがある

class ChargesController < ApplicationController 
    include CurrentCart 
    before_action :set_cart, only: [:new, :create] 
    #before_action :set_charge, only: [:create] 

    def new 
     #@charge = Charge.new 
    end 

    def create #METHOD IS CALLED AFTER PAYMENT IS MADE 
    # Amount in cents 

    @charge = Charge.new(charge_params) 
    @amount = (@cart.total_price * 735) 


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

    charge = Stripe::Charge.create(
     :customer => customer.id, 
     :amount  => @amount, 
     :description => 'Witch or Wizard', 
     :currency => 'usd' 
    ) 

    Cart.destroy(session[:cart_id]) 

    rescue Stripe::CardError => e 
     flash[:error] = e.message 
     redirect_to new_charge_path 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_charge 
     @charge = Charge.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def charge_params 
     params.permit(:first_name, :last_name, :address, :city, :state, :email, :total_price) 
    end 

end 

私はこのフォームを送信すると、私は私のset_charge方法に関するこのエラーを取得します:

は、私が何をやっている=

'ID' でチャージが見つかりませんでしたここで間違っている?ストライプ機能は正常に機能しますが、フォームに含まれる顧客情報をデータベースに保存しようとしています。

Started POST "/charges" for 127.0.0.1 at 2017-07-16 20:45:24 -0400 
Processing by ChargesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"9Kj986IJXX+NQuJ3msJelyYSYQfmQa+i+kmqdseoidR11mmt2T6xZrXi/WtgjAb2u6zs4GRF9nIr6/ZW3U4F/g==", "charges"=>{"first_name"=>"Matt", "last_name"=>"Cole", "address"=>"1 South Main St", "city"=>"Marietta", "state"=>"GA", "email"=>"[email protected]", "total_price"=>"4"}, "stripeToken"=>"tok_1AgNMTJG64O6iQtfUhLFVKZF", "stripeTokenType"=>"card", "stripeEmail"=>"[email protected]"} 
    Cart Load (0.2ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT ? [["id", 71], ["LIMIT", 1]] 
    LineItem Load (0.7ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ? [["cart_id", 71]] 
    Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ? [["id", 17], ["LIMIT", 1]] 
    CACHE (0.0ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT ? [["id", 71], ["LIMIT", 1]] 
    (0.1ms) begin transaction 
    CACHE (0.0ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ? [["cart_id", 71]] 
    SQL (0.3ms) DELETE FROM "line_items" WHERE "line_items"."id" = ? [["id", 135]] 
    SQL (0.1ms) DELETE FROM "carts" WHERE "carts"."id" = ? [["id", 71]] 
    (1.7ms) commit transaction 
    Rendering charges/create.html.erb within layouts/application 
    Rendered charges/create.html.erb within layouts/application (1.0ms) 
    User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 7], ["LIMIT", 1]] 
Completed 200 OK in 1529ms (Views: 61.8ms | ActiveRecord: 6.0ms) 

答えて

0

問題は何idがフォーム、これエラーを提出した後に送られていないということです。私は、フォームを送信する際

は、ここに私のログです。

しかし、あなたはChargeを作成し、そのidはまだ作成されていないので、その方法はまったく必要ありません。そして、さらに上の、あなたのcreateアクションの1行目に(set_chargeに設定されている)@chargeを置き換え、それは次のようになります。

@charge = Charge.new(charge_params) 

がエラーを修正するにはちょうどあなたのコントローラと、あなたからの次の行を削除します問題ないはずです。一般的には

before_action :set_charge, only: [:create] 

、あなただけで、つまり、レコードを更新するときにのみ(すなわち既存のレコードを見つけるため)、このようなコールバックを使用したいと思うでしょうおよびupdateアクション。

あなたのコントローラに、このようなアクションを追加する予定があれば、唯一のeditupdateアクションのために実行される(代わりにそれを削除する)上記の行を変更:提案のための

before_action :set_charge, only: [:edit, :update] 
+0

感謝。私はそれを試してみましたが、現在は私のcharge_paramsメソッドでエラーが発生しています: paramが見つからないか値が空です:charge – matt

+0

@matt 'form_tag'を使用しているので、送信された属性は' charge 'キーを使用しますが、強力なparamsメソッド(すなわち' charge_params')はそれを必要とします。それで 'require'を削除して、' params.permit(:first_name、:last_name、:address、:city、:state、:email、:total_price) 'のようにしてください。 – Gerry

+0

要求を削除しようとすると、エラーは表示されずにフォームが送信されますが、料金表には何も追加されません。 – matt

関連する問題