简单的表单不添加引导类

时间:2016-07-12 02:59:04

标签: ruby-on-rails twitter-bootstrap simple-form

我已经创建了一个新的Rails项目,将gem 'simple_form', '~> 3.2', '>= 3.2.1'添加到我的gemfile并配置我的项目以使用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"
  }
}

的Gemfile

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.text_field替换f.input。它与建议的bootstrap主题问题无关。

2 个答案:

答案 0 :(得分:1)

  

要开始使用Simple Form,您只需使用帮助器即可   提供:

<tbody>
  @foreach($work as $var)
    <tr>
      <td class="col-md-2">
    ...
    </tr>
  @endforeach
</tbody>

助手为<%= simple_form_for @user do |f| %> <%= f.input :username %> <%= f.input :password %> <%= f.button :submit %> <% end %> 而不是Rails助手,例如f.inputf.text_field等。请参阅Simple_Form UsageRails Form Helpers

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

enter image description here

答案 1 :(得分:0)

确保引导程序存储在正确的位置:

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