Rails 验证虚拟属性,设置表单错误。轨道广播#32
我基本上试图实现 Railscast #32 的解决方案,针对 Rails 3.0.7 进行了现代化
http://railscasts.com/episodes/32-time-in-text-field
class Task < ActiveRecord::Base
attr_accessible :description, :complete, :deadline
validate :deadline_string_no_errors
def deadline_string
self.deadline.to_s
end
def deadline_string=(deadline_str)
unless deadline_str.blank?
begin
self.deadline = Chronic.parse(deadline_str)
rescue
self.deadline = Time.parse(deadline_str)
rescue
@deadline_invalid = true
end
end
end
def deadline_string_no_errors
errors.add(:deadline_string, "Is Invalid") if @deadline_invalid
end
我想在通过控制台将字符串(例如“foobar”)放入#deadline_string=时设置验证错误或(预期的)表格输入。
我特别担心的是更严格的时间格式 Chronic.parse('2011-05-25 08:19:00 UTC')
这会引发 TypeError。在这种情况下,我想回退到 Time.parse,它可以理解这种格式。
此替代形式也不适用于 deadline_string="foobar"
作为初始调用。
# snip
def deadline_string=(deadline_str)
unless deadline_str.blank?
self.deadline = ( parse_time(deadline_str, Chronic) || parse_time(deadline_str, Time) )
end
end
private
def parse_time(string, parser)
begin
parser.parse(string)
rescue
@deadline_invalid = true
end
end
def deadline_string_no_errors
#snip
end
无论我如何尝试,似乎都无法进行第二次救援。截止日期属性也有一些奇怪之处,它在模式中指定为日期时间。
最终的解决方案是这样的,但它可能可以经受一些重构。我对在方法调用中设置 self.deadline 感到焦虑,但不确定为什么应该这样做。
Class Task < ActiveRecord::Base
attr_accessible: description, :complete, :deadline
validate :deadline_string_no_errors
def deadline_string
self.deadline.to_s
end
def deadline_string=(deadline_str)
unless deadline_str.blank?
self.deadline = ( parse_time(deadline_str, Chronic) || parse_time(deadline_str, Time) )
@deadline_invalid = true if self.deadline.nil?
end
end
private
def parse_time(string, parser)
begin
parser.parse(string)
rescue
end
end
def deadline_string_no_errors
errors.add(:deadline_string, "Is Invalid") if @deadline_invalid
end
欢迎重构,并将被标记为答案。
I'm basically attempting to implement the solution from Railscast #32, modernized for Rails 3.0.7
http://railscasts.com/episodes/32-time-in-text-field
class Task < ActiveRecord::Base
attr_accessible :description, :complete, :deadline
validate :deadline_string_no_errors
def deadline_string
self.deadline.to_s
end
def deadline_string=(deadline_str)
unless deadline_str.blank?
begin
self.deadline = Chronic.parse(deadline_str)
rescue
self.deadline = Time.parse(deadline_str)
rescue
@deadline_invalid = true
end
end
end
def deadline_string_no_errors
errors.add(:deadline_string, "Is Invalid") if @deadline_invalid
end
I want to set a validation error when a string, such as 'foobar' is put into #deadline_string=, either by console or (intended) form entry.
What I'm particularly concerned about is a more strict formatting of time, Chronic.parse('2011-05-25 08:19:00 UTC')
Which throws a TypeError. In that case I want to fallback to Time.parse, which can understand this formatting.
This alternative form also does not work with deadline_string="foobar"
as the initial call.
# snip
def deadline_string=(deadline_str)
unless deadline_str.blank?
self.deadline = ( parse_time(deadline_str, Chronic) || parse_time(deadline_str, Time) )
end
end
private
def parse_time(string, parser)
begin
parser.parse(string)
rescue
@deadline_invalid = true
end
end
def deadline_string_no_errors
#snip
end
No matter what I try it doesn't ever seem to get to the second rescue. There is also some strangeness around the deadline attribute, which is specified in the schema as a datetime.
The solution in the end was something like this, but it could probably stand to some refactoring. I'm anxious about setting self.deadline
in the method call, but not sure why I should be.
Class Task < ActiveRecord::Base
attr_accessible: description, :complete, :deadline
validate :deadline_string_no_errors
def deadline_string
self.deadline.to_s
end
def deadline_string=(deadline_str)
unless deadline_str.blank?
self.deadline = ( parse_time(deadline_str, Chronic) || parse_time(deadline_str, Time) )
@deadline_invalid = true if self.deadline.nil?
end
end
private
def parse_time(string, parser)
begin
parser.parse(string)
rescue
end
end
def deadline_string_no_errors
errors.add(:deadline_string, "Is Invalid") if @deadline_invalid
end
Refactoring is welcome, and will be marked as an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几件事:
产量为零,而且也不例外。
产生一个 Time 对象,而不是异常。 (编辑:在 Ruby 1.8.7 中)
当您使用救援时,您需要指定要救援的异常类型
A few of things:
yields nil and not an exception.
yields a Time object, not an exception. (Edit: In Ruby 1.8.7)
When you use a rescue, you need to specify the type of exception that you're rescuing