Rails:使用 simple_form 并集成 Twitter Bootstrap

发布于 2024-12-02 00:00:27 字数 182 浏览 2 评论 0原文

我正在尝试构建一个 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 技术交流群。

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

发布评论

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

评论(9

方觉久 2024-12-09 00:00:27

注意:此答案仅适用于 SimpleForm 2.0

config/initializers/simple_form.rb 中开始:

SimpleForm.form_class = nil
SimpleForm.wrapper_class = 'clearfix'
SimpleForm.wrapper_error_class = 'error'
SimpleForm.error_class = 'help-inline'

然后标签元素和输入之间缺少空格,您可以使用以下内容添加:

label {
  margin-right: 20px;
}

可能还有更多,但它是开始。

Note: This answer only applies to SimpleForm < 2.0

Start with this in config/initializers/simple_form.rb:

SimpleForm.form_class = nil
SimpleForm.wrapper_class = 'clearfix'
SimpleForm.wrapper_error_class = 'error'
SimpleForm.error_class = 'help-inline'

Then there's space missing between the label element and the input, which you can add with this:

label {
  margin-right: 20px;
}

There's probably more, but it's a start.

你对谁都笑 2024-12-09 00:00:27

Simple form 2.0 是 bootstrap-aware 的:

rails generate simple_form:install --bootstrap

Simple form 2.0 is bootstrap-aware:

rails generate simple_form:install --bootstrap
一绘本一梦想 2024-12-09 00:00:27

我最近遇到了同样的问题,并尝试了 bootstrap-sassformtastic-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!

我要还你自由 2024-12-09 00:00:27

我写了一个 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.

此刻的回忆 2024-12-09 00:00:27

这是完整的配置(config/initializers/simple_form.rb):

SimpleForm.setup do |config|
  config.hint_class = 'help-block'
  config.error_class = 'help-inline'
  config.wrapper_class = 'clearfix'
  config.wrapper_error_class = 'error'
  config.label_text = lambda { |label, required| "#{label} #{required}" }
  config.form_class = nil
end

Here's the full config (config/initializers/simple_form.rb):

SimpleForm.setup do |config|
  config.hint_class = 'help-block'
  config.error_class = 'help-inline'
  config.wrapper_class = 'clearfix'
  config.wrapper_error_class = 'error'
  config.label_text = lambda { |label, required| "#{label} #{required}" }
  config.form_class = nil
end
浮光之海 2024-12-09 00:00:27

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' } %>

白衬杉格子梦 2024-12-09 00:00:27

我发现这个,似乎工作正常https://github.com/rafaelfranca/simple_form-b​​ootstrap不知道紧密集成

I found this, seems working fine https://github.com/rafaelfranca/simple_form-bootstrap don't known about tight integration

梦在深巷 2024-12-09 00:00:27

如果您使用 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

听,心雨的声音 2024-12-09 00:00:27

在此railscast中: http://railscasts.com/episodes/329-more-on -twitter-bootstrap,您可以看到如何使用twitter bootstrap自定义simple_form(跳至3:05)。

终端:

rails g simple_form:install --bootstrap

model/_form.html.erb:

<%= simple_form_for @product, :html => { :class => 'form-horizontal' } do |f| %>
  <fieldset>
    <legend><%= controller.action_name.capitalize %> Product</legend>

    <%= f.input :name %>
    <%= f.input :price %>

    <div class="form-actions">
      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', products_path, :class => 'btn' %>
    </div>
  </fieldset>
<% end %>

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 :

rails g simple_form:install --bootstrap

model/_form.html.erb :

<%= simple_form_for @product, :html => { :class => 'form-horizontal' } do |f| %>
  <fieldset>
    <legend><%= controller.action_name.capitalize %> Product</legend>

    <%= f.input :name %>
    <%= f.input :price %>

    <div class="form-actions">
      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', products_path, :class => 'btn' %>
    </div>
  </fieldset>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文