0

我将 enumerated_attribute 与 formtastic ~> 1.2.3 与字段 :as => :enum 的“猴子补丁”一起使用,一切正常。

但是当我将 formtastic 更新到 2.0.2 版本时,出现了一条错误消息“Formtastic::UnknownInputError”。

有关更多详细信息,请参阅已添加到 /initialisers/formtastic.rb 的补丁:

module Formtastic #:nodoc:
  class SemanticFormBuilder #:nodoc:
    def enum_input(method, options)
      unless options[:collection]
        enum = @object.enums(method.to_sym)
        choices = enum ? enum.select_options : []
        options[:collection] = choices
      end
      if (value = @object.__send__(method.to_sym))
        options[:selected] ||= value.to_s
      else
        options[:include_blank] ||= true
      end
      select_input(method, options)
    end
  end
end

PS我试图将 SemanticFormBuilder 更改为 FormBuilder (正如我从新的 formtastic 文档中了解到的那样,所有自定义输入都有这样的变化),但我仍然收到错误

也许有人已经成功地一起使用了这些宝石?

4

1 回答 1

1

他们定义自定义字段的方式在 Formtastic 2.x 中完全改变了

您需要对内部 Formtastic 类进行子类化以获得您想要的。选择输入看起来像这样:

module FormtasticExtensions
  class EnumeratedInput < Formtastic::Inputs::SelectInput
    def collection
      # programmatically build an array of options in here and return them
      # they should be in this format:
      # [['name', 'value'],['name2', 'value2']]
    end
  end
end

在 Formtastic 初始化程序中包含该模块:

include FormtasticExtensions

这会给你一个领域:as => :enumerated,你应该很高兴。在我的情况下(其他一些自定义字段),它选择当前选项,但您可能需要调整代码才能正常工作。

您也可以将集合传入:

f.input :thing, :as => :select, :collection => your_collection, :label_method => :your_name, :value_method => :your_id

于 2011-10-15T18:31:17.197 回答