我希望用户能够选择或输入时间(例如:“您花费多长时间刷牙早上?“)和”创建“或”更新“操作,我想将时间转换为在发布/放入数据库之前的秒数。在POST/PUT到数据库之前将时间(MM:SS)转换为浮点数(秒)
我有一个模块中的方法,现在(努力学习如何使用模块),但我不知道这是否是适合这种情况... ...
float到时间的视图(作品)
<%= MyModule.float_to_time(task.task_length) %>
模块
module MyModule
def self.float_to_time(secs) # task.task_length (view)
...
...
end
def self.time_to_float(tim) # :task_length (controller)
m = tim.strftime("%M").to_f
s = tim.strftime("%S.%2N").to_f
t = ((m*60) + s)
return t.round(2)
end
end
控制器
def create
# "time_to_float" (Convert task_time to seconds float)
# Create new task after conversion
@task = Task.new(params[:task])
respond_to do |format|
if @task.save
format.html { redirect_to @task, notice: 'Task was successfully created.' }
format.json { render json: @event, status: :created, location: @task }
else
format.html { render action: "new" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
我该怎么做后转换前(或认沽)的分贝?
另外,我应该将这些方法移至application_controller,还是我的模块正常?
- 感谢您的任何帮助。
不要忘了'//'不会在Ruby中开始评论。 '#'确实。您可能想编辑您的帖子以反映这一点。 – 2013-03-04 13:41:08
哎呦,脑屁。感谢Charles。 – econduck 2013-03-04 13:53:55