我在使用Rails 4.2創建具有多個模型的表單時遇到問題。 我正在創建一個表單,允許用戶使用自定義條形付款方式一次性訂購禮品卡。 我嘗試了不同的方式使用form_tag而不是simple_form_for和simple_fields_for,我嘗試了所有在這個網站上提供的解決方案,但我找不到一個出路!Rails 4.2:使用多個模型的表單 - 缺少param或值
我的格式如下:
=simple_form_for @giftcard, url: gift_cards_path, html: {"accept-charset" => "UTF-8", :autocomplete => "off", :enctype => "multipart/form-data", :method => "post" } do |f|
(...)
= field_set_tag 'Delivery mode' do
.form-row
= f.label :delivery_mode, 'She receive the mail'
= f.radio_button :delivery_mode, 'email_to_recipient'
= f.label :delivery_mode, 'I receive the mail'
= f.radio_button :delivery_mode, 'email_to_giver'
= f.label :delivery_mode, 'Delivery with in box with a jewel'
= f.radio_button :delivery_mode, 'gift_box', id: 'gift_box'
= field_set_tag 'Who is the gift for ?' do
.form-row
= f.input :recipient_first_name, label: 'First Name'
.form-row
= f.input :recipient_last_name, label: 'Last Name'
.form-row
= f.input :recipient_email, placeholder: '[email protected]', label: 'Son adresse email'
.form-row
= f.input :message, placeholder: 'Your message', label: 'Son message'
= f.simple_fields_for :payment do |p|
.form-row
= p.input :card_number, label: 'Numéro de carte'
.form-row
= p.input :card_cvc, input_html: { "size" => 4, "data-stripe" => "cvv" }, label: 'CVV'
.form-row
= p.input :card_expiration_month, input_html: { "placeholder" => "MM", "size" => 2, "data-stripe" => "exp-month" }
= p.input :card_expiration_year, input_html: { "placeholder" => "YYYY", "size" => 4, "data-stripe" => "exp-year" }
.form-row
= f.button :submit, 'Send your gift !'
我的控制器如下:
def new
@giftcard = GiftCard.new
@payment = Payment.new
end
def create
@giftcard = GiftCard.new(giftcard_params)
@payment = Payment.new(creditcard_params)
@giftcard.amount += 9.90 if @giftcard.gift_box?
respond_to do |format|
if @giftcard.save && @payment.process_payment
code_coupon = CodeCoupon.create(name: giftcard.coupon, value: 0, category: 'membership', origin: 'gift')
if giftcard.delivery_mode == 'gift_box'
UserMailer.prepare_gift_box(@giftcard)
UserMailer.giftcard_recipe(@giftcard)
elsif @giftcard.delivery_mode == 'email_to_giver'
UserMailer.send_gift_card(@giftcard, @giftcard.giver_email)
UserMailer.giftcard_recipe(@giftcard)
elsif @giftcard.delivery_mode == 'email_to_recipient'
UserMailer.send_gift_card(@giftcard, @giftcard.recipient_email)
UserMailer.giftcard_recipe(@giftcard)
end
format.html { redirect_to @giftcard, notice: 'Votre carte cadeau à bien été créée et vous a été envoyé par email' }
format.json { render :show, status: :created, location: @giftcard }
else
format.html { render :new, locals: { errors: @giftcard.errors.full_messages } }
format.json { render json: @giftcard.errors, status: :unprocessable_entity }
end
end
end
private
def giftcard_params
params.require(:gift_card).permit(:recipient_email, :recipient_first_name, :recipient_last_name, :giver_email, :giver_phone, :giver_first_name, :giver_last_name, :message, :amount, :delivery_mode, :delivery_first_name, :delivery_last_name, :delivery_street, :delivery_city, :delivery_zip)
end
def creditcard_params
params.require(:payment).permit(:card_number, :card_cvc, :card_expiration_month, :card_expiration_year, :card_token)
end
而陷入困境的模型:
class Payment
include ActiveModel::Model
attr_accessor :card_number, :card_cvc, :card_expiration_month, :card_expiration_year, :card_token
validates :card_number, presence: true
validates :card_cvc, presence: true
validates :card_expiration_month, presence: true
validates :card_expiration_year, presence: true
def payment_process
Stripe::Charge.create(
amount: amount * 100,
description: "paiement unique",
source: card_token,
currency: 'eur'
)
end
end
當我嘗試驗證表單,它會返回以下錯誤: ActionController :: ParameterMissing - param is missi納克或值是空的:付款: ActionPack的(4.2.8)LIB/action_controller /金屬/ strong_parameters.rb:251:在require' app/controllers/gift_cards_controller.rb:56:in
creditcard_params' 應用程序/控制器/ gift_cards_controller.rb:13:在`創建」
如果我刪除的creditcard_params方法的需要,我點擊提交,我在日誌中得到: 不允許的參數:支付 不允許的參數:UTF8,authenticity_token,GIFT_CARD,提交 (0.1毫秒)BEGIN (爲0.2ms)ROLLBACK
編輯
個在這裏,我在日誌中收到的參數:
Started POST "/gift_cards" for ::1 at 2017-10-06 10:17:42 +0200
Processing by GiftCardsController#create as HTML
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"UB1keTN7YNDhaEBdIT6cYf/qrwr/QxCpFEYwvGDBLWmYvZiVjXxFg+bQ9vaPAfQTx32d/D7jHnShsTmsqMwLrA==",
"gift_card"=>{
"recipient_first_name"=>"",
"recipient_last_name"=>"",
"recipient_email"=>"",
"message"=>"",
"giver_first_name"=>"",
"giver_last_name"=>"",
"giver_email"=>"",
"giver_phone"=>"",
"delivery_first_name"=>"",
"delivery_last_name"=>"",
"delivery_street"=>"",
"delivery_city"=>"",
"delivery_zip"=>"",
"payment"=>{
"card_number"=>"",
"card_cvc"=>"",
"card_expiration_month"=>"",
"card_expiration_year"=>""
}
},
"commit"=>"Send your gift !"
}
預先感謝您的幫助,
問候
可以共享該要在控制器中接收的參數? –
嗨@VaibhavDhoke謝謝你的回答,我把參數放在編輯部分! –