Formtastic~> 2.0.2和enumerated_attribute gem,Rails 3.1.1

时间:2011-10-14 16:18:32

标签: formtastic rails-3.1 enumerated-types

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

但是当我将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

P.S。我试图将SemanticFormBuilder更改为FormBuilder(我从新的formtastic文档中了解到所有自定义输入都有这样的更改),但我仍然收到错误

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

1 个答案:

答案 0 :(得分: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