RailsCasts32 Time in Text Field 时间类型的输入域

The tasks index page.

Above is an application that displays a list of tasks. Clicking on the edit link for a task takes us to the edit page for that task. The edit page uses the datetime_select helper method to render the tasks as a list of drop down lists.

The edit page for a task.

改变5个下拉框并不是更新日期的最好方式。如果在输入框中能填入信息对用户来说是更容易的。应用可以解析输入内容将他们回填到数据库中。

将下拉框改成输入框意味着输入域并没有直接的映射到数据库中。我们不得不为Task模型创建一个虚拟属性,命名为due_at_string。

ruby
<h1>Edit Task</h1>
<% form_for @task do |form| %>
<ol class="formList">
  <li>
    <%= form.label :name, "Name" %>
    <%= form.text_field :name %>
  </li>
  <li>
    <%= form.label :due_at_string, "Due at" %>
    <%= form.text_field :due_at_string %>
  </li>
  <li>
    <%= submit_tag "Edit task" %>
  </li>
</ol>
<% end %>

在模型中,为虚拟属性添加setter、getter方法,模型中的代码如下:

ruby
class Task < ActiveRecord::Base
  belongs_to :project
  validates_presence_of :name

  def due_at_string
    due_at.to_s(:db)
  end

  def due_at_string=(due_at_str)
    self.due_at = Time.parse(due_at_str)
  end
end

setter方法利用Time的parse方法将输入转化为Time。

有时需要比Time.parse 提供更多功能的函数,可使用Chronic gem,Task模型中添加的代码如下:


ruby
require 'chronic'
def due_at_string=(due_at_str)
    self.due_at = Chronic.parse(due_at_str)
  end

处理异常

如果传入一个不能解析的字符串,Chronic将返回nil。Time.parse将抛出 ArgumentError 异常 .显然,需向setter中添加rescue。我们将向类中加入一个实例变量,如果存入数据库中时抛出异常,则置为true;添加validate方法,当time不能被解析时,将向task中添加一个error。

ruby
def due_at_string=(due_at_str)
  self.due_at = Time.parse(due_at_str)
rescue ArgumentError
  @due_at_invalid = true
end

def validate
  errors.add(:due_at, "is invalid") if @due_at_invalid
end

If we try to pass an invalid date now, the error will be caught and added to the model’s list of errors. It’s worth noting that Time.parse will replace missing or completely invalid parts of the string passed with the equivalent part from Time.now, so if we pass a completely invalid value like "Hello, world!" the due date will be set to the current time. An exception will only be raised if an invalid date such as "32-12-2009" is passed.

The edit page will now show an error if an invalid date is passed.

We now have a much better way of inputting dates and times into Rails applications. Whether we choose to use the Chronic gem or the built-in time parsing methods we can handle a wide range of date formats from user input.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值