Ruby on rails 轨道,路线时间必须大于两站之间的时间
我需要一些关于Rails限制的帮助。 我创建了一个交通站点,整个路线的持续时间应该大于两站之间的时间。这是我的模型文件“section.rb”Ruby on rails 轨道,路线时间必须大于两站之间的时间,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,我需要一些关于Rails限制的帮助。 我创建了一个交通站点,整个路线的持续时间应该大于两站之间的时间。这是我的模型文件“section.rb” section.rb class ValidatoreOffset < ActiveModel::Validator def validate(record) if record.controllo_offset record.errors[:base] << 'Error! Orari
section.rb
class ValidatoreOffset < ActiveModel::Validator
def validate(record)
if record.controllo_offset
record.errors[:base] << 'Error! Orario fermate intermedie maggiore dell orario della tratta di percorrenza'
end
end
end
class Section < ActiveRecord::Base
has_many :departures
has_many :reservations
has_many :stops
attr_accessible :travel_time, :location_arrival, :type_of_section, :start_location, :id, :id_departure, :stop_id
validates :type_of_section, inclusion: { in: %w(festivo feriale scolastico giornaliero)} , :allow_nil => false
delegate :offset, :to => :stop, prefix: true, :allow_nil => true
validates_with ValidatoreOffset
def controllo_offset
if section.stop.offset >= section.travel_time
return true
end
end
end
offset是Stop的一个属性,表示两次停止之间的时间。
这有什么不对
编辑:
完整错误消息:
Started POST "/sections" for ::1 at 2015-09-30 16:50:02 +0200
Processing by SectionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ywua04ZmGKKsv0OvkrealyAZ4s4iQ7IF1nFg41aYNpdtlXj9kPy4rRC/wkWzH5EoG+d23/QSvEhF4vdErkTNuA==", "section"=>{"start_location"=>"kbh", "location_arrival"=>"gfx", "type_of_section"=>"gfehmw,", "travel_time"=>"17", "stop_id"=>"6"}, "commit"=>"Create Section"}
Unpermitted parameter: stop_id
(0.2ms) BEGIN
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 18ms (ActiveRecord: 2.8ms)
NameError (undefined local variable or method `section' for #<Section:0x007f8a093691e8>):
app/models/section.rb:23:in `controllo_offset'
app/models/section.rb:3:in `validate'
app/controllers/sections_controller.rb:30:in `block in create'
app/controllers/sections_controller.rb:29:in `create'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.7ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.5ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.8ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (20.5ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (12.8ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (27.2ms)
当此方法运行时
def controllo_offset
if section.stop.offset >= section.travel_time
return true
end
end
它在一个节实例中运行。它不知道节是什么-它只是认为它是一个未定义的变量或方法,这就是错误告诉您的。您可能希望改用self,它是运行该方法的对象。乙二醇
def controllo_offset
if self.stop.offset >= self.travel_time
return true
end
end
由于controllo_offset是一种实例方法,要访问截面对象,应使用self:
我认为停止方法也是错误的,因为您的部分有许多停止。您的模型应该是。关于访问模型中属性的部分应替换为模型中的self
class Section < ActiveRecord::Base
has_many :departures
has_many :reservations
has_many :stops
attr_accessible :travel_time, :location_arrival, :type_of_section, :start_location, :id, :id_departure, :stop_id
validates :type_of_section, inclusion: { in: %w(festivo feriale scolastico giornaliero)} , :allow_nil => false
delegate :offset, :to => :stop, prefix: true, :allow_nil => true
validates_with ValidatoreOffset
def controllo_offset
if self.stop.offset >= self.travel_time
return true
end
end
end
这有什么不对?这会引起错误吗?你的问题是什么?它说的是未定义的局部变量或方法'section',现在它告诉我'Undefined method'stop',因为我希望你在对实例调用该方法之前知道模型有一个实例方法。我看不到你的代码库,所以我不知道节实例是否应该有停止方法。。但是我应该用什么来代替“停止”?我想你想要停止。最后。但您需要验证节是否至少有一个停止点。我认为您应该花点时间了解activeRecord的工作原理:
class Section < ActiveRecord::Base
has_many :departures
has_many :reservations
has_many :stops
attr_accessible :travel_time, :location_arrival, :type_of_section, :start_location, :id, :id_departure, :stop_id
validates :type_of_section, inclusion: { in: %w(festivo feriale scolastico giornaliero)} , :allow_nil => false
delegate :offset, :to => :stop, prefix: true, :allow_nil => true
validates_with ValidatoreOffset
def controllo_offset
if self.stop.offset >= self.travel_time
return true
end
end
end