我希望用户能够选择或输入时间(例如:“您早上刷牙多长时间?”)并且在“创建”或“更新”操作中,我想转换时间在发布/放入数据库之前的几秒钟内浮动。
我现在有一个模块中的方法(试图学习如何使用模块),但我不确定它是否适合这种情况......
浮动时间查看(作品)
<%= 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,还是我的模块可以?
- 谢谢你的帮助。