Ruby on rails NoMethodError at/orders/new未定义的方法'validate_on_create';
我目前在/orders/new获得了一个Ruby on rails NoMethodError at/orders/new未定义的方法'validate_on_create';,ruby-on-rails,ruby-on-rails-3,validation,methods,model,Ruby On Rails,Ruby On Rails 3,Validation,Methods,Model,我目前在/orders/new获得了一个命名错误 未定义的#的“创建时验证”方法。我的代码似乎很简单,应该是功能性的,但事实并非如此 笔记 在此错误之前,我在/orders/new处得到了错误NoMethodError 未定义NilClass:Class的方法“model\u name”,因此我将@order的表单更改为订单的表单。新的 下面是我的代码: 订单/新订单 错误 NoMethodError at/orders/new 未定义的方法“在创建时验证”# Order.method_缺失 a
命名错误
未定义的#
的“创建时验证”方法。我的代码似乎很简单,应该是功能性的,但事实并非如此
笔记
在此错误之前,我在/orders/new处得到了错误NoMethodError
未定义NilClass:Class的方法“model\u name”,因此我将@order的表单更改为订单的表单。新的
下面是我的代码:
订单/新订单
错误
NoMethodError at/orders/new
未定义的方法“在创建时验证”#
Order.method_缺失
activerecord(3.2.13)lib/active_record/dynamic_matchers.rb,第55行
app/models/order.rb,第8行
app/models/order.rb,第1行
ActiveSupport::Dependencies.load\u文件中的块
activesupport(3.2.13)lib/active_support/dependencies.rb,第469行
ActiveSupport::Dependencies.new_constants_in
activesupport(3.2.13)lib/active_support/dependencies.rb,第639行
ActiveSupport::Dependencies.load\u文件
activesupport(3.2.13)lib/active_support/dependencies.rb,第468行
ActiveSupport::Dependencies.require\u或\u load
activesupport(3.2.13)lib/active_support/dependencies.rb,第353行
ActiveSupport::Dependencies.load\u缺少\u常量
activesupport(3.2.13)lib/active_support/dependencies.rb,第502行
Object.const_中的块丢失
activesupport(3.2.13)lib/active_support/dependencies.rb,第192行
Object.const_丢失
activesupport(3.2.13)lib/active_support/dependencies.rb,第190行
ActiveSupport::Dependencies.load\u缺少\u常量
activesupport(3.2.13)lib/active_support/dependencies.rb,第514行
ActionView.const_中的块丢失
activesupport(3.2.13)lib/active_support/dependencies.rb,第192行
ActionView.const_缺失
activesupport(3.2.13)lib/active_support/dependencies.rb,第190行
ActiveSupport::Dependencies.load\u缺少\u常量
activesupport(3.2.13)lib/active_support/dependencies.rb,第514行
ActionView中的块::CompiledTemplates.const_缺失
activesupport(3.2.13)lib/active_support/dependencies.rb,第192行
ActionView::CompiledTemplates.const_缺失
activesupport(3.2.13)lib/active_support/dependencies.rb,第190行
##_应用程序查看、订单、新增、html、erb、2575536509094834、41759220
app/views/orders/new.html.erb,第4行
ActionView中的块::模板#渲染
actionpack(3.2.13)lib/action_view/template.rb,第145行
ActiveSupport::Notifications.instrument
activesupport(3.2.13)lib/active_support/notifications.rb,第125行
ActionView::模板#渲染
actionpack(3.2.13)lib/action_view/template.rb,第143行
ActionView::TemplateRenderer#render_模板中的块(2级)
actionpack(3.2.13)lib/action_view/renderer/template_renderer.rb,第47行
ActionView中的块::TemplateRenderer#仪器
actionpack(3.2.13)lib/action_view/renderer/abstract_renderer.rb,第38行
ActiveSupport::Notifications.instrument中的块
activesupport(3.2.13)lib/active_support/notifications.rb,第123行
ActiveSupport::Notifications::Instrumenter#instrument
activesupport(3.2.13)lib/active_support/notifications/instrumenter.rb,第20行
ActiveSupport::Notifications.instrument
activesupport(3.2.13)lib/active_support/notifications.rb,第123行
ActionView::TemplateRenderer#仪器
actionpack(3.2.13)lib/action_view/renderer/abstract_renderer.rb,第38行
ActionView::TemplateRenderer#render_模板中的块
actionpack(3.2.13)lib/action_view/renderer/template_renderer.rb,第46行
非常感谢您提供的任何见解。在rails 2.0.3参考后,已删除创建上的验证
使用validate:method\u name,:on=>:create
因此,对于您的情况,以下操作应该有效
validate :validate_card, :on => :create
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
validate :validate_card, :on => :create