我有这个限制的问题!我无法比较两个日期,控制台给我这个错误:
undefined method `<' for nil:NilClass
这是我到目前为止编写的代码。
# app/models/reservation.rb
class CountValidator < ActiveModel::Validator
def validate(record)
if (record.second || record.first)
record.errors[:base]<< ' error '
end
end
end
class DepartureValidator < ActiveModel::Validator
def validate(record)
if (record.date_trip < Date.today)
record.errors[:base]<< ' error '
end
end
end
class Reservation < ActiveRecord::Base
validates_with DepartureValidator
validates_with CountValidator
belongs_to :dep ,:class_name => 'Stop', :foreign_key => 'dep_id'
belongs_to :arr ,:class_name => 'Stop',:foreign_key => 'arr_id'
belongs_to :route
belongs_to :user
delegate :CountStop, :to => :route, prefix: true, :allow_nil => false
delegate :city ,:to => :arr, :allow_nil => false
delegate :city ,:to => :dep, :allow_nil => false
def division
return Reservation.select{|r| r.route_id == route_id && r.date_trip == date_trip && r.id != id }
end
def second
if (class_point == 2)
y=division.select{ |l| l.class_point == 2 }.count
if(y+1 > route.train.II_class_seats)
return true
end
end
return false
end
def first
if (class_point == 1)
y=division.select{ |l| l.class_point == 1 }.count
if(y+1 > route.train.I_class_seats)
return true
end
end
return false
end
end
我不明白问题出在哪里。我的一个朋友写了相同的代码,但它对他来说很完美。
编辑:
这是完整的消息错误:
Started POST "/reservations" for 127.0.0.1 at 2015-09-02 15:04:13 +0200
Processing by ReservationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PzfuWIWXqb6C3cCiI+fLendTadwR/kG/++3q6TRcgxLAUGyD1eFluBZB2SZZB3f0ZH6QiRyoj6py+3a6gfOg8A==", "reservation"=>{"class_point"=>"1", "route_id"=>"2", "dep_id"=>"4", "arr_id"=>"4"}, "commit"=>"Create reservation!"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.2ms) begin transaction
(0.1ms) rollback transaction
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.4ms)
NoMethodError (undefined method `<' for nil:NilClass):
app/models/reservation.rb:12:in `validate'
app/controllers/reservations_controller.rb:33:in `block in create'
app/controllers/reservations_controller.rb:32:in `create'
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.8ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.8ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (24.9ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.8ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.5ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.6ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (16.8ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.6ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (36.2ms)
答案 0 :(得分:0)
您使用DepartureValidator验证的预订记录没有&#39; date_trip&#39;属性(如您的日志所示)。将validates_with DepartureValidator
替换为validates_with DepartureValidator, if: "date_trip.present?"
OR
将验证者更改为:
class DepartureValidator < ActiveModel::Validator
def validate(record)
if (record.date_trip && record.date_trip < Date.today)
record.errors[:base]<< ' error '
end
end
end