0
我有一個Rails窗體,其中包含一個腳本來生成一個條紋支付表單。表格中嵌套的是fields_for
標籤。這裏是我的全部費用/ new.html.erb文件:從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>
這裏是我的收費控制器:
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)
感謝您的建議。儘管我嘗試了,但現在我的charge_params方法出現錯誤: param丟失或值爲空:charge – matt
@matt由於您使用的是form_tag,所以發送的屬性不會被分組到「charge」 '鍵,但是你的強參數方法(即'charge_params')需要它。因此,請嘗試刪除'require',將這一行保留下來:'params.permit(:first_name,:last_name,:address,:city,:state,:email,:total_price)'。 – Gerry
當我嘗試刪除require時,表單提交時沒有出現任何錯誤,但沒有任何內容添加到收費表中。 – matt