Rails:使用 simple_form 并集成 Twitter Bootstrap
我正在尝试构建一个 Rails 应用程序,并且 simple_form 看起来是一个非常有用的宝石。问题是我使用 twitter bootstrap css 来进行样式设置,而 simple_form 不允许您指定 html 的布局。
谁能告诉我如何将 simple_form html 符合 bootstrap css 想要的格式?
I'm trying to build a rails app and simple_form looks to be a really useful gem. Problem is that I am using twitter bootstrap css to do the styling and the simple_form doesn't allow you to specify the layout of the html.
Can anyone tell me how I can conform the simple_form html into the format bootstrap css wants?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
注意:此答案仅适用于 SimpleForm 2.0
从
config/initializers/simple_form.rb
中开始:然后标签元素和输入之间缺少空格,您可以使用以下内容添加:
可能还有更多,但它是开始。
Note: This answer only applies to SimpleForm < 2.0
Start with this in
config/initializers/simple_form.rb
:Then there's space missing between the label element and the input, which you can add with this:
There's probably more, but it's a start.
Simple form 2.0 是 bootstrap-aware 的:
Simple form 2.0 is bootstrap-aware:
我最近遇到了同样的问题,并尝试了 bootstrap-sass 和 formtastic-bootstrap。
它使用与 formattastic 代码完全相同的代码,甚至按预期显示错误消息。
bootstrap-sass
还可以与 Rails 3.1 Asset Pipeline 和 Rails 3.0 配合使用。formtastic-bootstrap
已使用 RSpec 进行测试,因此我认为这是一个不错的方法!I recently had the same problem and tried out the combination of bootstrap-sass and formtastic-bootstrap.
It works with the exactly same code as the code for formtastic and shows even error messages as expected.
bootstrap-sass
also works with Rails 3.1 Asset Pipeline and Rails 3.0.formtastic-bootstrap
is tested with RSpec so I think this is a nice way to go!我写了一个 gem 来做到这一点。它不是 simple_form,但它应该可以并行工作:twitter_bootstrap_form_for。
I wrote a gem to do exactly this. It's not simple_form, but it should work fine side-by-side: twitter_bootstrap_form_for.
这是完整的配置(config/initializers/simple_form.rb):
Here's the full config (config/initializers/simple_form.rb):
simple_form 允许使用 css 类(传入
:html => {:class => nil}
将仅生成一个“simple_form”类。)。注:这是在 2011 年 7 月 25 日添加的,因此许多现有的下载和文档都没有它。您还可以将其包装在指定样式的 div 中。
代码来设置单个元素的样式
您还可以始终使用诸如
<%= f.input :username, :label_html =>; 之类的 。 {:类=> 'my_class' } %>
simple_form allows for a css class (Passing in
:html => {:class => nil}
will result in only a "simple_form" class.).n.b. This was added on 7/25/2011 so many existing downloads and documentation will not have it. You could also wrap it in a div that specifies a style.
You can also always style an individual element with code such as
<%= f.input :username, :label_html => { :class => 'my_class' } %>
我发现这个,似乎工作正常https://github.com/rafaelfranca/simple_form-bootstrap不知道紧密集成
I found this, seems working fine https://github.com/rafaelfranca/simple_form-bootstrap don't known about tight integration
如果您使用 SimpleForm 1.5,gem 的作者会在此处为您提供所需的配置说明: https://github.com/plataformatec/simple_form/wiki/Twitter-Bootstrap-integration
If you use SimpleForm 1.5 the authors of the gem provide you with the required configuration instructions here: https://github.com/plataformatec/simple_form/wiki/Twitter-Bootstrap-integration
在此railscast中: http://railscasts.com/episodes/329-more-on -twitter-bootstrap,您可以看到如何使用twitter bootstrap自定义simple_form(跳至3:05)。
终端:
model/_form.html.erb:
In this railscast: http://railscasts.com/episodes/329-more-on-twitter-bootstrap, you can see how to customize simple_form with twitter bootstrap (skip to 3:05).
terminal :
model/_form.html.erb :