1
我試圖實現用戶可以購買物品的表單。爲此,他們需要使用信用卡。爲了幫助他們,我安裝了ActiveMerchant Gem。這是我的CreditCard表單。ActiveMerchant :: Billing :: CreditCard的未定義方法'model_name':
<%= form_for @credit_card do |f| %>
<div class = "inner-form">
<p class = "heading">Payment Information</p>
<p>
<%= f.label :card_type, "Card Type:*" %></br>
<%= select_tag(:card_type, options_for_select([['Visa', 1], ['MasterCard', 2], ['American Express', 3], ['Discover', 4]])) %>
</p>
<p>
<%= f.label :card_number, "Card Number:*" %></br>
<%= f.text_field :card_number %>
</p>
<p>
<%= f.label :card_verification, "Security Code:*" %></br>
<%= f.text_field :card_verification %>
</p>
<p>
<%= f.label :card_expires_on, "Expiration Date:*" %></br>
<%= select_tag(:card_expires_on, options_for_select([['01-January', 1], ['02-February', 2], ['03-March', 3], ['04-April', 4], ['05-May', 5],
['06-June', 6], ['07-July', 7], ['08-August', 8], ['09-September', 9], ['10-October', 10], ['11-November', 11], ['12-December', 12]])) %>
<%= select_tag(:card_expires_on, options_for_select([['2014', 1], ['2015', 2], ['2016', 3], ['2017', 4], ['2018', 5], ['2019', 6], ['2020', 7],
['2021', 8], ['2022', 9], ['2023', 10], ['2024', 11], ['2025', 12], ['2026', 13], ['2027', 14], ['2028', 15], ['2029', 16], ['2030', 17]])) %>
</p>
<%= f.submit "Charge My Credit Card and Process My Order", :class => "add-to-cart" %></br></br>
這裏是我的CreditCardsController
class CreditCardsController < ApplicationController
def new
@credit_card = ActiveMerchant::Billing::CreditCard.new
end
def create
@credit_card = ActiveMerchant::Billing::CreditCard.new(credit_card_params)
if @credit_card.save
flash[:notice] = "Thank you for your purchase!"
redirect_to root_path
else
flash[:danger] = "You have entered invalid information"
redirect_to :back
end
end
private
def credit_card_params
params.require(:credit_card).permit(:card_type, :card_number, :card_verification, :card_expires_on)
end
end
信用卡式型號
class CreditCard < ActiveRecord::Base
attr_accessor :card_number, :card_verification
validate :validate_card, on: :create
private
def validate_card
unless credit_card.valid?
credit_card.errors.full_messages.each { |message| errors[:base] << "error" }
end
end
def credit_card
@credit_card ||= ActiveMerchant::Billing::CreditCard.new(
:type => card_type,
:number => card_number,
:verification_value => card_verification,
:month => card_expires_on.month,
:year => card_expires_on.year,s
:first_name => first_name,
:last_name => last_name
)
end
end
我就遇到了這個消息。
NoMethodError in CreditCards#new
undefined method 'model_name' for ActiveMerchant::Billing::CreditCard:Class
我已經安裝了最新版本ActiveMerchant的(1.45)
此錯誤消息並沒有給我很多的信息來解決這個問題。
您可能正在使用帶有'ActiveMerchant :: Billing :: CreditCard'對象參數的'form_for'構建器。 'form_for'希望它的參數類定義'model_name' – avlazarov 2014-12-04 22:06:16
是的,我沒有正確地格式化我的代碼,所以我離開了form_for行 – 2014-12-04 22:09:40