【问题标题】:Creating a custom input select in formtastic在formtastic中创建自定义输入选择
【发布时间】:2014-07-08 04:27:36
【问题描述】:

我想为使用自定义集合的 formtasic 创建自定义输入

我的收藏被MyModule::Categories.all返回

我正在扩展 SelectInput

class CategoriesSelectInput < Formtastic::Inputs::SelectInput
  def select_html
    collection = MyModule::Categories.all
    builder.select(input_name, collection, input_options, input_html_options)
  end

结束

但格式似乎丢失了,我哪里出错了?

【问题讨论】:

    标签: ruby-on-rails formtastic formbuilder


    【解决方案1】:

    有点晚了..但是试试这个方法:

    class CategoriesSelectInput < Formtastic::Inputs::SelectInput
      def to_html
        collection = MyModule::Categories.all
    
        builder.input input_name, as: :select, collection: collection, input_html: input_html_options
    
      end
    end
    

    【讨论】:

      【解决方案2】:

      无需扩展选择输入。 Formatstic 对提供自定义集合有很好的支持。详细阅读它的文档:https://github.com/justinfrench/formtastic

      简而言之,你可以这样做:

      <%= f.input :some_id, :as => :select, :collection => MyModule::Categories.all %>
      

      【讨论】:

      • 我知道我能做到。但我想做&lt;%= f.input, :something, as: MyCustomSelect %&gt;
      猜你喜欢
      • 2023-03-24
      • 2021-10-10
      • 1970-01-01
      • 2012-01-22
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      相关资源
      最近更新 更多