rails使用bootstrap

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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了。

以上是关于rails使用bootstrap的主要内容,如果未能解决你的问题,请参考以下文章

BootStrap有用代码片段(持续总结)

BootStrap实用代码片段(持续总结)

导航栏折叠在 Rails 5/Bootstrap 3 上不起作用

Bootstrap 多选下拉菜单,在 Rails 上带有搜索栏

Bootstrap Carousel 没有作为 Rails 应用程序的一部分移动

带有 WebPack 的 Bootstrap4 的 DataTables 没有集成在 Rails 6 中?