ActiveMerchant :: Billing :: CreditCard:Class的未定义方法'model_name'

时间:2014-12-04 21:56:32

标签: ruby-on-rails ruby credit-card activemerchant

我正在尝试实施一个用户可以购买商品的表单。为此,他们需要使用信用卡。为了帮助他们,我安装了ActiveMerchant宝石。这是我的CreditCard表格。

<%= 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

CreditCard模型

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)

此错误消息并未提供大量信息来解决此问题。

1 个答案:

答案 0 :(得分:0)

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