Rails 验证虚拟属性,设置表单错误。轨道广播#32

发布于 2024-11-09 20:22:51 字数 2175 浏览 6 评论 0原文

我基本上试图实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有一腔孤勇 2024-11-16 20:22:51

有几件事:

$ Chronic.parse 'foo'

产量为零,而且也不例外。

$ Time.parse 'foo'

产生一个 Time 对象,而不是异常。 (编辑:在 Ruby 1.8.7 中)

当您使用救援时,您需要指定要救援的异常类型

rescue SyntaxError, NameError => boom

A few of things:

$ Chronic.parse 'foo'

yields nil and not an exception.

$ Time.parse 'foo'

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

rescue SyntaxError, NameError => boom
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文