【问题标题】:Formtastic, own :as input typeFormtastic,自己的:作为输入类型
【发布时间】:2010-04-28 17:25:00
【问题描述】:

如何将我自己的字段类型添加到 formtastic 中?

例如,我需要一个自定义的日期时间输入,我想要这样的东西:

<%= f.input :start_date , :as => :my_date %>

这显然行不通,因为 formtastic 不知道 :my_date(只有 :boolean、:string、:datetime 等等...)

但是我怎样才能添加额外的输入类型呢?

【问题讨论】:

    标签: ruby forms input formtastic


    【解决方案1】:

    需要添加自定义输入法:

    class MyCustomFormtasticFormBuilder < Formtastic::SemanticFormBuilder
      protected
      def my_date_input(method, options)
        basic_input_helper(:text_field, :my_date, method, options)
      end
    end
    

    这非常适合新的 HTML5 输入类型。你可以这样使用它:

    <% form_form @model, :builder => MyCustomFormtasticFormBuilder  do |f| %>
       <%= f.input :start_date, :as => :my_date
    <% end %>
    

    【讨论】:

    • 完美答案。我唯一的补充是,您需要为我们配置 Formtastic 的 MyCustomFormtasticFormBuilder 而不是 Formtastic::SemanticFormBuilder,这可以在 Formtastic 提供的配置初始化程序中完成。
    【解决方案2】:

    不要再继承 Formtastic::FormBuilder

    之前在 Formtastic 1.x 中建议子类化 Formtastic::FormBuilder 以添加 您自己的输入。这在 Formtastic 2 中不再推荐,并且不会按预期工作。

    https://github.com/justinfrench/formtastic

    http://justinfrench.com/notebook/formtastic-2-preview-custom-inputs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      相关资源
      最近更新 更多