ruby - Rails, route time has to be greater than the time between two stops -
i need here restriction in rails. created transport site , duration of whole route should greater time between 2 stops. model file 'section.rb'
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 attribute of stop indicates time between 2 stops. wrong that?
edit: full error message:
started post "/sections" ::1 @ 2015-09-30 16:50:02 +0200 processing sectionscontroller#create 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' #<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)
when method runs
def controllo_offset if section.stop.offset >= section.travel_time return true end end
it's running within section instance. doesn't know "section" - thinks it's undefined variable or method, error telling you. want use self
instead, object method running in. eg
def controllo_offset if self.stop.offset >= self.travel_time return true end end
Comments
Post a Comment