简单形式不加 bootstrap 类

Simple form not adding bootstrap classes

我创建了一个新的 Rails 项目,将 gem 'simple_form', '~> 3.2', '>= 3.2.1' 添加到我的 gem 文件并将我的项目配置为使用 Bower,而不是通过 Bower 添加 Bootstrap。

即使我使用了 rails generate simple_form:install --bootstrap,表单元素也没有得到 Bootstrap css 类.

我在页面呈现后检查了 HTML 并且 Bootstrap css/js 在那里。我需要使用 Bootstrap gem 而不是 Bower 包吗??

这是 .bowerrc 文件:

{
  "directory": "vendor/assets/components"
}

bower.json

{
  "name": "TesteKlipbox",
  "authors": [
    "Juliano Nunes"
  ],
  "description": "",
  "main": "",
  "license": "MIT",
  "homepage": "",
  "private": true,
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "vendor/assets/components",
    "test",
    "tests"
  ],
  "dependencies": {
    "bootstrap": "^3.3.6"
  }
}

宝石文件

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc


gem 'simple_form', '~> 3.2', '>= 3.2.1'

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

表格代码:

<%= simple_form_for(@noticia, html: { class: 'form-horizontal' }) do |f| %>
  <% if @noticia.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@noticia.errors.count, "error") %> erros impediram que esta notícia fosse salva:</h2>

      <ul>
      <% @noticia.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :titulo %><br>
    <%= f.text_field :titulo %>
  </div>
  <div class="field">
    <%= f.label :texto %><br>
    <%= f.text_area :texto %>
  </div>
  <div class="field">
    <%= f.label :data %><br>
    <%= f.datetime_select :data %>
  </div>
  <div class="field">
    <%= f.label :tags %><br>
    <%= f.text_field :tags %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

更新

另一个用户已经回答了这个问题。我的错误是我没有用 f.input 替换 f.text_field。它与建议的 bootstrap 主题问题无关。

确保 bootstrap 存储在正确的位置:

bootstrap.min.css 以及 bootstrap.css,

bootstrap.min.jsbootstrap.js,

都需要分别位于app/assets/stylesheetsapp/assets/javascripts

*= require bootstrap 添加到 app/assets/stylesheets/application.css 并且,

//= require bootstrap 添加到 app/assets/javascripts/application.js

To start using Simple Form you just have to use the helper it provides:

<%= simple_form_for @user do |f| %>
  <%= f.input :username %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

助手是 f.input 而不是 Rails 助手,例如 f.text_fieldf.text_area 等。参见 Simple_Form Usage and Rails Form Helpers

下图显示第一个输入使用 f.text_field,第二个使用 f.input