Rails 在 Rails 中最方便集成使用 Bootstrap 的方式

Chorder · 2018年02月12日 · 最后由 taichis 回复于 2018年08月03日 · 3891 次阅读

创建项目

rails new BootstrapProject

创建模型

rails g scaffold xxx --skip-stylesheets

运行迁移

rake db:migrate

--------- 如果项目和模型都已经建立好了并已经运行了迁移,那么可以省略以上步骤,直接进入下面的流程 ---------

在 Gemfile 中添加 bootstrap,这里使用 twitter-bootstrap-rails

gem 'twitter-bootstrap-rails'
gem 'therubyracer'
gem 'less-rails'

bundle

bundle install

安装 bootstrap

rails g bootstrap:install

在模型上运用 bootstrap

rails g bootstrap:themed xxx -f

注:xxx 可以是任意的模型,例如模型名称是 Article,那么这里的语句就是:

rails g bootstrap:themed Articles 

还要在 application.js 中加上引用,否则 bootstrap 的一些按钮会失效:

//= require jquery

//= require twitter/bootstrap

Enjoy it~

bootstrap 官方有 gem 包。

官方包 bootstrap-sass X

第三方包 twitter-bootstrap-rails ✓

不错!可以只用这个包的 helper!

哥可否請問一下,twitter-bootstrap-rails 似乎是引用 bootstrap3,如果我已經有專案使用這個方式開發,要如何升級為 bootstrap4 呢?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号