0
对于使用模块来说很新颖。我无法让我的Module方法在将用户输入字符串(“1:48.55”)转换为浮点数之前验证它并将其保存到数据库。不知道我做错了什么......在模块中转换属性时'未定义的方法'
的config/application.rb中
config.autoload_paths += %W(#{config.root}/lib/modules/)
lib/modules目录/ sport_time.rb
module SportTime
extend ActiveSupport::Concern
attr_accessor :goal_time
included do
before_validation :sporty_save
end
def self.stringify_race(secs)
m = (secs/60).floor
s = (secs - (m*60))
sprintf("%02d:%.2f\n",m,s)
end
private
def sporty_save
self.goal_time = self.goal_time.floatify_race(goal_time) ---(line 12)---
end
def floatify_race(str)
dirty = str.split(":")
min = dirty[0]
sec = dirty[1]
seconds = (min.to_i * 60) + sec.to_f
seconds.round(4)
end
end
应用程序/模型/ event.rb
class Event < ActiveRecord::Base
include SportTime
validates_presence_of :event_type, :race_length, :course, :goal_time, :user_id
attr_accessible :event_type, :race_length, :course, :goal_time, :user_id
belongs_to :user
end
DB/seed.rb
Event.create(
:event_type => 'Run',
:race_length => 800,
:course => 'outdoor',
:goal_time => '01:48.55',
:user_id => user_one.id
)
错误:
rake aborted!
undefined method `floatify_race' for "01:48.55":String
/Users/myname/work/projectname/lib/modules/sport_time.rb:12:in `sporty_save'
我在做什么错?