格式〜> 2.0.2 和 enumerated_attribute gem,Rails 3.1.1
我使用 enumerated_attribute 和 formattastic ~> 1.2.3 字段使用“monkey patch”: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 文档中了解到,所有自定义输入都有这样的更改),但我仍然收到错误
也许有人已经成功地一起使用了这些宝石?
I used enumerated_attribute with formtastic ~> 1.2.3 with "monkey patch" for field :as => :enum and everything worked fine.
But when I updated formtastic to 2.0.2 version appeared an error with message "Formtastic::UnknownInputError".
For more details here is patch, that was added to /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. I tried to change SemanticFormBuilder to FormBuilder (as I understood from new formtastic documentation there was such change for all custom inputs), but I was still getting error
Maybe anybody already used these gems together successfully?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自定义字段的定义方式在 Formtastic 2.x 中已完全改变,
您需要对内部 Formtastic 类进行子类化才能获得您想要的内容。选择输入看起来像这样:
将该模块包含在 Formtastic 初始值设定项中:
包括 FormtasticExtensions
这将为您提供一个字段
:as =>; :enumerated
你应该可以开始了。就我而言(其他一些自定义字段),它选择当前选项,但您可能需要调整代码才能工作。您也可以将集合传递到:
f.input :thing, :as => :选择,:集合=> your_collection, :label_method =>; :你的名字, :值方法 => :你的_id
They way custom fields are defined has changed completely in Formtastic 2.x
You need to subclass the internal Formtastic classes to get what you want. A select input would look something like this:
Include the module in the Formtastic initializer:
include FormtasticExtensions
and this will give you a field
:as => :enumerated
and you should be good to go. In my case (some other custom field) it selects the current option, but you may need to tweak the code for yours to work.You could also just pass the collection in:
f.input :thing, :as => :select, :collection => your_collection, :label_method => :your_name, :value_method => :your_id