将 Formtastic 设置为 Rails 3 默认表单生成器,可能吗?

发布于 2024-10-06 14:10:58 字数 806 浏览 0 评论 0原文

application.rb 中类似这样的内容:

# Configure application generators
config.app_generators do |g|
  g.form_builder Formtastic::SemanticFormBuilder
end

如果这样做,当我尝试构建模型时会收到错误:

Expected Thor class, got Formtastic::SemanticFormBuilder 

是否可以将 Formtastic 设置为默认表单生成器?


已更新

我尝试过简单表单,它真的很棒(感谢nathanvda)。 DSL 与 Formtastic 几乎相同。对我来说唯一重要的区别是自定义按钮标签。在formtastic中,可以使用资源文件(formtastic.yml)为相同的模型和操作设置不同的标签。有时这是必要的,例如在设计视图中。但即使在这种情况下,从形式转换到简单形式也不需要花费任何成本,因为可以通过这种非常简单的方式来做到这一点:

= f.submit t("customized_button_label")

现在关于最初的问题。当我安装简单表单时,它会在 lib/templates/haml/scaffold 目录中创建模板,该模板将与脚手架一起使用。直截了当。

Something like this in application.rb:

# Configure application generators
config.app_generators do |g|
  g.form_builder Formtastic::SemanticFormBuilder
end

If I do so I get an error when I try to scaffold a model:

Expected Thor class, got Formtastic::SemanticFormBuilder 

Is it possible to set Formtastic as default form builder?


Updated.

I have tried Simple forms and it's really awesome (Thanks to nathanvda). The DSL is almost the same as Formtastic has. The only important difference for me is in customizing button labels. In formtastic it's possible to use resource file (formtastic.yml) to set different labels for the same model and action. Sometimes it's necessary, for example in Devise views. But it costs nothing to switch from formtastic to simple forms even in this case as it's possible to do it in this pretty simple way:

= f.submit t("customized_button_label")

Now about the original question. When I installed simple forms it creates template in lib/templates/haml/scaffold directory which will be used with scaffold. Straightforward.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2024-10-13 14:10:58

我对 formattastic 不太确定,要么它是直接开箱即用的,所以不需要配置;或者根本没有。

但我所知道的是: simple_form 确实提供了脚手架,甚至是可配置的,这非常棒。 formattastic 和 simple_form 之间的 DSL 几乎相同,但 simple_form 的配置级别要好得多。您可以完全控制如何构建表单,可以完全控制如何将单个字段转换为 html。太棒了。

您可以在此处找到快速介绍。

I am not entirely sure about formtastic, either it does this straight out of the box, so not configuration needed; or not at all.

But what i do know: simple_form does provide scaffolding, even configurable which is totally awesome. The DSL between formtastic and simple_form is close to identical, but with simple_form the level of configuration is much better. You have total control how a form should be scaffolded, you have total control how a single field is turned into html. Pretty awesome.

You can find a quick introduction here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文