2014-12-04 58 views
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)

此错误消息并没有给我很多的信息来解决这个问题。

+0

您可能正在使用带有'ActiveMerchant :: Billing :: CreditCard'对象参数的'form_for'构建器。 'form_for'希望它的参数类定义'model_name' – avlazarov 2014-12-04 22:06:16

+0

是的,我没有正确地格式化我的代码,所以我离开了form_for行 – 2014-12-04 22:09:40

回答

0

ActiveMerchant::Billing::CreditCard对象不能在form_for中使用,除非你猴子补丁类。 一个解决方案是使用form_for :credit_card

相关问题