如何覆盖Rails的select form helper?

时间:2012-11-11 16:53:16

标签: ruby-on-rails ruby ruby-on-rails-3

我试图像这样覆盖Rails的表单选择助手:

def select(method, choices, options = {}, html_options = {})
  html_options.reverse_merge!(:disabled => true)
  super(method, choices, options = {}, html_options = {})
end

目标是disable所有选择标记。

不幸的是,它根本不起作用。选择框根本不会被禁用,也不会抛出任何错误。我已经验证了该方法在表单中被正确调用,因此不会出现问题。

有人可以告诉我这里缺少什么吗?

感谢您的任何指示。

1 个答案:

答案 0 :(得分:1)

当您将选项选项发送到super时,您正在重置选项选项。

def select(method, choices, options = {}, html_options = {})
  html_options.reverse_merge!(:disabled => true)

  # Don't do this!  By doing this you're *always* sending empty objects for
  # options and html_options to super.
  #super(method, choices, options = {}, html_options = {})

  # Do this!
  super(method, choices, options, html_options)
end