如果一个model中含有时间字段,那么add a new object的时候,我们可以使用datetime_select helper method to render it as a list of drop down lists, 就是,年月日小时,分都显示成一个droplist,可以选择。
当然,并不是所有人都喜欢这种输入方式。如果用户可以通过文本的方式输入时间,岂不是更好?然后由我们的程序进行解析然后存储到数据库中。
这么做意味着我们显示出来的字段跟数据库中实际存储的字段不一致。我们可以通过虚拟属性来记录这个新的string字段。
首先修改一些view中的代码
<%form_for @task do |form|%>
<%= form.text_field :due_at_string%>
<%= submit_tag "Edit task"%>
<%end%>
下面我们就要为这个虚拟属性添加getter and setter 方法了。
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
谢天谢地,Time这个class已经给我提供了parse这个类方法,它将字符串解析成Time对象。
但是有时候,parse不能满足我们的要求,我们需要更多的时间格式,我们可以使用Chronic gem。
gem install chronic 即可。
使用时,要在你的类的开头加入 require 'chronic'
然后把Time.parse 替换成 Chronic.parse
Chronic 比 Time的一点好处是, 他可以 转换相对时间,比如tomorrow, monday, 甚至next tuesday at 8pm 之类的。
很强大把!
如果碰到不能parse的,Chronic会返回nil。
但是如果Time.parse 碰到不能parse的,它会返回一个ArgumentError Exception.
因此,如果我们使用的是Time的parse,就要添加一个rescue block,来对付这异常。
代码如下:
# def due_at_string=(due_at_str)
# self.due_at = Time.parse(due_at_str)
# rescue ArgumentError
# @due_at_invalid = true
# end
如果碰到这个异常,那么我们把实例变量置为true来标记。
然后在validate method中(这个方法在属性写入数据库之前会被自动执行的。)
如果这个变量为true,就往对象的errors属性中加入一个新的error(这个errors对象是一直存在着的。(从ActiveRecord继承的?也许把))
# def validate
# errors.add(:due_at, "is invalid") if @due_at_invalid
# end #第一个参数是字段名称,第二个参数是要显示出来的错误信息字符串。
还有最后需要注意的一点,对于Time.parse方法,如果他的参数是一个完全不合法的参数,或者根本没有参数,那么rails不会生成一个异常,而是会使用默认的Time.now作为替代,比如我们传入一个字符串(hello world), 那么不会报错,而是会存储成current time。
但是如果传入的是32-12-2009, 那么就肯定会报错了。
当然,并不是所有人都喜欢这种输入方式。如果用户可以通过文本的方式输入时间,岂不是更好?然后由我们的程序进行解析然后存储到数据库中。
这么做意味着我们显示出来的字段跟数据库中实际存储的字段不一致。我们可以通过虚拟属性来记录这个新的string字段。
首先修改一些view中的代码
<%form_for @task do |form|%>
<%= form.text_field :due_at_string%>
<%= submit_tag "Edit task"%>
<%end%>
下面我们就要为这个虚拟属性添加getter and setter 方法了。
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
谢天谢地,Time这个class已经给我提供了parse这个类方法,它将字符串解析成Time对象。
但是有时候,parse不能满足我们的要求,我们需要更多的时间格式,我们可以使用Chronic gem。
gem install chronic 即可。
使用时,要在你的类的开头加入 require 'chronic'
然后把Time.parse 替换成 Chronic.parse
Chronic 比 Time的一点好处是, 他可以 转换相对时间,比如tomorrow, monday, 甚至next tuesday at 8pm 之类的。
很强大把!
如果碰到不能parse的,Chronic会返回nil。
但是如果Time.parse 碰到不能parse的,它会返回一个ArgumentError Exception.
因此,如果我们使用的是Time的parse,就要添加一个rescue block,来对付这异常。
代码如下:
# def due_at_string=(due_at_str)
# self.due_at = Time.parse(due_at_str)
# rescue ArgumentError
# @due_at_invalid = true
# end
如果碰到这个异常,那么我们把实例变量置为true来标记。
然后在validate method中(这个方法在属性写入数据库之前会被自动执行的。)
如果这个变量为true,就往对象的errors属性中加入一个新的error(这个errors对象是一直存在着的。(从ActiveRecord继承的?也许把))
# def validate
# errors.add(:due_at, "is invalid") if @due_at_invalid
# end #第一个参数是字段名称,第二个参数是要显示出来的错误信息字符串。
还有最后需要注意的一点,对于Time.parse方法,如果他的参数是一个完全不合法的参数,或者根本没有参数,那么rails不会生成一个异常,而是会使用默认的Time.now作为替代,比如我们传入一个字符串(hello world), 那么不会报错,而是会存储成current time。
但是如果传入的是32-12-2009, 那么就肯定会报错了。
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 825the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8611. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9141. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8801.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7551. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 9011. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 7051. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11081. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 747in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 778in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 668again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7101. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 738whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7511. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 727There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 660start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 645a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 723in integration test, you can te ...
相关推荐
crawl_update_time = scrapy.Field() def get_insert_sql(self): insert_sql = """ insert into lagou_job(title, url, salary, job_city, work_years, degree_need, job_type, publish_time
Real-Time Video Compression: techniques and Algorithms can be used as a text for graduate students and researchers working in the area of real-time video compression. In addition, the book serves as ...
in various time series domains under a unified taxonomy of DNNs for TSC. We also provide an open source deep learning framework to the TSC community where we implemented each of the compared ...
Allows create and fill data in design-time and save data in dfm file of the Form. Allows keep record in the manner of trees. Each record can have record elements-branches and itself be an ...
DEF ChangeString Text { string "ChangeString" } ] } Script { url "vrmlscript: function initialize() { }" } #ROUTE RotSensor.touchTime TO RotationScript.clicked ROUTE RotSensor.touchTime TO ...
- fixed bug in ODF export with UTF8 encoding of the Creator field - fixed bug in XML export with processing special characters in strings - fixed bug in ODF export with properties table:number-columns...
v8.0.1 RAD Studio 10.3 Rio is supported Support of UPPER and LOWER ...time zone when connecting in the Direct mode is fixed Bug with using an extended string in DML statements in Oracle 12 is fixed Bug ...
v8.0.1 RAD Studio 10.2 Tokyo is supported Support of UPPER and LOWER...time zone when connecting in the Direct mode is fixed Bug with using an extended string in DML statements in Oracle 12 is fixed Bug ...
time = scrapy.Field() # 招聘时间 ``` ### 3. 编写Spider逻辑 编辑`field.py`文件,实现具体的爬虫逻辑: ```python # get_Field/spiders/field.py import scrapy from get_Field.items import GetItem from ...
Written by experts in their field, Pro series books from Apress give you the hard–won solutions to problems you will face in your professional programming career. About the Author Matthew MacDonald...
Hardware concerns are examined in a chapter, contributed by Steven Molnar and Henry Fuchs, on advanced architectures for real-time, high performance graphics. The comprehensive topic coverage ...
Bug with mapping a TEXT field to ftWideMemo in Delphi is fixed Bug with SQL statements containing a CONTAINS predicate in the WHERE clause is fixed Bug with application freezing when executing a ...
Bug with mapping a TEXT field to ftWideMemo in Delphi is fixed Bug with SQL statements containing a CONTAINS predicate in the WHERE clause is fixed Bug with application freezing when executing a ...
:litter_in_bin_sign: 通过单击单元格清除部分代码; :red_apple: 在iOS上支持“快速粘贴短信代码”。 并为Android粘贴自定义代码; :high_voltage: TextInput ref支持; :hammer_and_wrench: 高度可定制的。 可...
you will have solid experience implementing some of the most interesting and relevant data mining techniques available today, and you will have achieved a greater fluency in the important field of ...
6)....Fixed: Empty "Count" field/column is now displayed as "1" in Viewer 7)....Fixed: Generic names with "," could not be decoded in Viewer 8)....Fixed: Updated Windows 10 detection for latest builds...
SCGCQ00362808 (DFCT) - MegaCli 32 Crashes in Windows in specific system SCGCQ00364411 (DFCT) - Segmentation Fault while running discard preserved cache in megacli Installation Commands: ===========...
- ADD: In TFlexPanel.DefaultLinkPoint property added Size field that lets define visual size of the connection points of the flex-connectors. Initialized with DefaultLinkPointSize constant in the ...
// delay (in 1/100s of a second) UINT m_nDisposal; // disposal method }; #pragma pack(1) // turn byte alignment on enum GIFBlockTypes { BLOCK_UNKNOWN, BLOCK_APPEXT, BLOCK_COMMEXT, BLOCK_...