rails使用bootstrap

在Gemfile文件中添加'bootstrap-sass',再运行bundle install

gem 'bootstrap-sass'

在config/application.rb添加一行代码,让bootstrap-sass和asset pipeline兼容

  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true
    #让bootstrap-sass和asset pipeline兼容
    config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
  end

要使用bootstrap还需要在app/assets/stylesheets目录下创建一个css文件:custom.css.scss

内容为:

@import "bootstrap-sprockets"
@import "bootstrap"

另外在app/assets/javascripts/application.js文件中添加一行

//= require bootstrap

这样才能使用bootstrap的javascript的组件。

现在就可以开始使用bootstrap了。