简体   繁体   English

Rails:使用simple_form并集成Twitter Bootstrap

[英]Rails: Using simple_form and integrating Twitter Bootstrap

I'm trying to build a rails app and simple_form looks to be a really useful gem. 我正在尝试构建一个rails应用程序,而simple_form看起来是一个非常有用的宝石。 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. 问题是我使用twitter bootstrap css来做样式,而simple_form不允许你指定html的布局。

Can anyone tell me how I can conform the simple_form html into the format bootstrap css wants? 谁能告诉我如何将simple_form html符合bootstrap css想要的格式?

Note: This answer only applies to SimpleForm < 2.0 注意:此答案仅适用于SimpleForm <2.0

Start with this in config/initializers/simple_form.rb : 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. 可能还有更多,但这是一个开始。

简单形式2.0是引导感知:

rails generate simple_form:install --bootstrap

I recently had the same problem and tried out the combination of bootstrap-sass and formtastic-bootstrap . 我最近遇到了同样的问题,尝试了bootstrap-sassformtastic-bootstrap的组合 It works with the exactly same code as the code for formtastic and shows even error messages as expected. 它使用与formtastic的代码完全相同的代码,并按预期显示甚至错误消息。

bootstrap-sass also works with Rails 3.1 Asset Pipeline and Rails 3.0. bootstrap-sass也适用于Rails 3.1 Asset Pipeline和Rails 3.0。 formtastic-bootstrap is tested with RSpec so I think this is a nice way to go! formtastic-bootstrap用RSpec测试,所以我认为这是一个很好的方法!

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 . 它不是simple_form,但它应该并行工作: twitter_bootstrap_form_for

simple_form allows for a css class (Passing in :html => {:class => nil} will result in only a "simple_form" class.). simple_form允许一个css类(传入:html => {:class => nil}只会产生一个“simple_form”类。)。
nb This was added on 7/25/2011 so many existing downloads and documentation will not have it. nb这是在2011年7月25日添加的,因此很多现有的下载和文档都没有。 You could also wrap it in a div that specifies a style. 您也可以将其包装在指定样式的div中。

You can also always style an individual element with code such as 您也可以使用代码来设置单个元素的样式

<%= f.input :username, :label_html => { :class => 'my_class' } %>

Here's the full config (config/initializers/simple_form.rb): 这是完整的配置(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

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

如果你使用SimpleForm 1.5,gem的作者会在这里为你提供所需的配置说明: https//github.com/plataformatec/simple_form/wiki/Twitter-Bootstrap-integration

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). 在这个railscast: http: //railscasts.com/episodes/329-more-on-twitter-bootstrap中,您可以看到如何使用twitter bootstrap自定义simple_form(跳至3:05)。

terminal : 终奌站 :

rails g simple_form:install --bootstrap

model/_form.html.erb : 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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM