現在、 を取得していNoMethodError at /orders/new
undefined method 'validate_on_create' for #<Class:0x000000047fde70>
ます。私のコードはシンプルで機能するはずですが、そうではありません。
ノート
このエラーの前にエラーが発生したため、次のNoMethodError at /orders/new
undefined method 'model_name' for NilClass:Class
ように変更form_for @order
しましたform_for Order.new
以下は私のコードです:
注文/新規
<% form_for Order.new do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</p>
<p>
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</p>
<p>
<%= f.label :card_type %><br />
<%= f.select :card_type, [["Visa", "visa"], ["MasterCard", "master"], ["American Express", "american_express"]] %>
</p>
<p>
<%= f.label :card_number %><br />
<%= f.text_field :card_number %>
</p>
<p>
<%= f.label :card_verification, "Card Verification Value (CVV)" %><br />
<%= f.text_field :card_verification %>
</p>
<p>
<%= f.label :card_expires_on %><br />
<%= f.date_select :card_expires_on, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
</p>
<p>
<%= f.submit "Submit" %></p>
<% end %>
コントローラ
class OrdersController < ApplicationController
def new
@order = Order.new
end
def create
@order = current_cart.build_order(params[:order])
@order.ip_address = request.remote_ip
if @order.save
if @order.purchase
render :action => "success"
else
render :action => "failure"
end
else
render :action => 'new'
end
end
end
モデル
class Order < ActiveRecord::Base
attr_accessible :card_expires_on, :card_type, :cart_id, :first_name, :ip_address, :last_name
belongs_to :cart
has_many :transactions, :class_name => "OrderTransaction"
attr_accessor :card_number, :card_verification
validate_on_create :validate_card
def purchase
response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
cart.update_attribute(:purchased_at, Time.now) if response.success?
response.success?
end
def price_in_cents
(cart.total_price*100).round
end
private
def purchase_options
{
:ip => ip_address,
:billing_address => {
:name => "",
:address1 => "",
:city => "",
:state => "",
:country => "",
:zip => ""
}
}
end
def validate_card
unless credit_card.valid?
credit_card.errors.full_messages.each do |message|
errors.add_to_base message
end
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,
:first_name => first_name,
:last_name => last_name
)
end
end
ルート
resources :carts
resources :orders
get 'cart' => 'carts#show', :as => 'current_cart'
エラー
NoMethodError at /orders/new
undefined method `validate_on_create' for #<Class:0x00000005104ca8>
Order.method_missing
activerecord (3.2.13) lib/active_record/dynamic_matchers.rb, line 55
<class:Order>
app/models/order.rb, line 8
<top (required)>
app/models/order.rb, line 1
block in ActiveSupport::Dependencies.load_file
activesupport (3.2.13) lib/active_support/dependencies.rb, line 469
ActiveSupport::Dependencies.new_constants_in
activesupport (3.2.13) lib/active_support/dependencies.rb, line 639
ActiveSupport::Dependencies.load_file
activesupport (3.2.13) lib/active_support/dependencies.rb, line 468
ActiveSupport::Dependencies.require_or_load
activesupport (3.2.13) lib/active_support/dependencies.rb, line 353
ActiveSupport::Dependencies.load_missing_constant
activesupport (3.2.13) lib/active_support/dependencies.rb, line 502
block in Object.const_missing
activesupport (3.2.13) lib/active_support/dependencies.rb, line 192
Object.const_missing
activesupport (3.2.13) lib/active_support/dependencies.rb, line 190
ActiveSupport::Dependencies.load_missing_constant
activesupport (3.2.13) lib/active_support/dependencies.rb, line 514
block in ActionView.const_missing
activesupport (3.2.13) lib/active_support/dependencies.rb, line 192
ActionView.const_missing
activesupport (3.2.13) lib/active_support/dependencies.rb, line 190
ActiveSupport::Dependencies.load_missing_constant
activesupport (3.2.13) lib/active_support/dependencies.rb, line 514
block in ActionView::CompiledTemplates.const_missing
activesupport (3.2.13) lib/active_support/dependencies.rb, line 192
ActionView::CompiledTemplates.const_missing
activesupport (3.2.13) lib/active_support/dependencies.rb, line 190
#<Class:0x00000004d71be0>#_app_views_orders_new_html_erb___2575536509094394834_41759220
app/views/orders/new.html.erb, line 4
block in ActionView::Template#render
actionpack (3.2.13) lib/action_view/template.rb, line 145
ActiveSupport::Notifications.instrument
activesupport (3.2.13) lib/active_support/notifications.rb, line 125
ActionView::Template#render
actionpack (3.2.13) lib/action_view/template.rb, line 143
block (2 levels) in ActionView::TemplateRenderer#render_template
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb, line 47
block in ActionView::TemplateRenderer#instrument
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb, line 38
block in ActiveSupport::Notifications.instrument
activesupport (3.2.13) lib/active_support/notifications.rb, line 123
ActiveSupport::Notifications::Instrumenter#instrument
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb, line 20
ActiveSupport::Notifications.instrument
activesupport (3.2.13) lib/active_support/notifications.rb, line 123
ActionView::TemplateRenderer#instrument
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb, line 38
block in ActionView::TemplateRenderer#render_template
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb, line 46
どんな洞察も大歓迎です。