【问题标题】:How to use simple_form and formtastic (active_admin) side by side?如何并排使用 simple_form 和 formtastic (active_admin)?
【发布时间】:2012-11-23 18:26:25
【问题描述】:

我有一个使用 active_admin gem 的 rails 项目。这个 active_admin 取决于 formtastic gem。但是,我想在我的应用程序中使用 simple_form 而不是 formtastic(在 active_admin 之外的所有部分)。

问题:simple_form 和 formtastic 都声明了“simple_form_for”帮助器,并且通常具有非常相似的 DSL。

如何在我的应用程序中使用 simple_form,同时仍然保留 active_admin(及其 formtastic)?

如果你问自己,为什么我需要这么麻烦的设置:我使用 twitter bootstrap,simple_form 很好地支持 bootstrap,而 formtastic 不支持。

【问题讨论】:

  • 据我所知,formtastic(至少 2.2.1+)没有定义 simple_form_for 方法,它定义了 semantic_form_for 方法。

标签: ruby-on-rails simple-form formtastic


【解决方案1】:

我认为现在(2015+),确保SimpleFormFormtastic 的兼容性(包括ActiveAdmin 依赖项)的最简单和最合适的方法是将您的命名空间中的自定义SimpleForm输入:

# If needed, you can namespace your custom inputs in a module and tell `SimpleForm`
# to look for their definitions in this module. This can avoid conflicts with other
# form libraries (like `Formtastic`) that look up the global context to find inputs
# definition too.

# app/inputs/custom_inputs/numeric_input.rb
module CustomInputs
  class NumericInput < SimpleForm::Inputs::NumericInput
    def input_html_classes
      super.push 'no-spinner'
    end
  end
end


# config/initializers/simple_form.rb
config.custom_inputs_namespaces << 'CustomInputs'

查看SimpleFormdocsCustom Inputs部分了解更多详情。

【讨论】:

  • 谢谢,好主意!我会尝试一下,一旦再次出现这样的问题。现在我通过摆脱 ActiveAdmin 并使用 rails_admin (github.com/sferik/rails_admin) 和 intercom.io 的组合解决了我的问题。我对这两种解决方案没有类似的问题。
  • 这就是我们解决类似问题的方式,是的,只是为了 active_admin 工作而将我所有的输入都填充到命名空间中很糟糕。我宁愿选择其他方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
相关资源
最近更新 更多