twitter-bootstrap-rails

by seyhunak

Twitter Bootstrap for Rails 6.0, Rails 5 - Rails 4.x Asset Pipeline

4.5K Stars 1.0K Forks Last release: Not found 1.0K Commits 4 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Twitter Bootstrap for Rails 6, 5 and 4 Asset Pipeline

Bootstrap is a toolkit from Twitter designed to kickstart development of web apps and sites. It includes base CSS and HTML for typography, forms, buttons, tables, grids, navigation, and more.

twitter-bootstrap-rails project integrates Bootstrap CSS toolkit for Rails Asset Pipeline (Rails 6, Rails 5 and Rails 3.x versions are supported)

Gem Version Dependency Status Code Climate Coverage Status GitHub stars GitHub forks GitHub issues

OpenCollective OpenCollective

Screencasts

Installing twitter-bootstrap-rails, generators, usage and more

Screencasts provided by RailsCasts (Ryan Bates)

Twitter Bootstrap Basics in this episode you will learn how to include Bootstrap into Rails application with the twitter-bootstrap-rails gem.

More on Twitter Bootstrap in this episode continues on the Bootstrap project showing how to display flash messages, add form validations with SimpleForm, customise layout with variables, and switch to using Sass. (Note: This episode is pro episode)

Installing the Gem

The Twitter Bootstrap Rails gem can provide the Bootstrap stylesheets in two ways.

The plain CSS way is how Bootstrap is provided on the official website.

The Less way provides more customisation options, like changing theme colors and provides useful Less mixins for your code, but requires the Less gem and the Ruby Racer Javascript runtime (not available on Microsoft Windows).

Installing the Less stylesheets

To use Less stylesheets, you'll need the less-rails gem, and one of JavaScript runtimes supported by CommonJS.

Include these lines in the Gemfile to install the gems from RubyGems.org:

gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails"

or you can install from latest build;

gem 'twitter-bootstrap-rails', :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git'

Then run

bundle install
from the command line:
bundle install

Then run the bootstrap generator to add Bootstrap includes into your assets:

rails generate bootstrap:install less

If you need to skip coffeescript replacement into app generators, use:

rails generate bootstrap:install --no-coffeescript

Installing the CSS stylesheets

If you don't need to customize the stylesheets using Less, the only gem you need is the

twitter-bootstrap-rails
gem:
gem "twitter-bootstrap-rails"

After running

bundle install
, run the generator:
rails generate bootstrap:install static

If your Rails server is running, make sure to restart it.

Generating layouts and views

You can run following generators to get started with Bootstrap quickly.

Layout (generates Bootstrap compatible layout) - (Haml and Slim supported)

Usage:

rails g bootstrap:layout [LAYOUT_NAME]

Themed (generates Bootstrap compatible scaffold views.) - (Haml and Slim supported)

Usage:

rails g bootstrap:themed [RESOURCE_NAME]

Example:

rails g scaffold Post title:string description:text
rake db:migrate
rails g bootstrap:themed Posts

Notice the plural usage of the resource to generate bootstrap:themed.

Using with Less

Bootstrap was built with Preboot, an open-source pack of mixins and variables to be used in conjunction with Less, a CSS preprocessor for faster and easier web development.

Using stylesheets with Less

You have to require Bootstrap LESS (bootstrapandoverrides.css.less) in your application.css

/*
 *= require bootstrap_and_overrides
 */

/* Your stylesheets goes here... */

To use individual components from bootstrap, your bootstrapandoverrides.less could look like this:

// Core variables and mixins
@import "twitter/bootstrap/variables.less";
@import "twitter/bootstrap/mixins.less";

// Reset and dependencies @import "twitter/bootstrap/normalize.less"; @import "twitter/bootstrap/print.less"; //@import "twitter/bootstrap/glyphicons.less"; // Excludes glyphicons

// Core CSS @import "twitter/bootstrap/scaffolding.less"; @import "twitter/bootstrap/type.less"; @import "twitter/bootstrap/code.less"; @import "twitter/bootstrap/grid.less"; @import "twitter/bootstrap/tables.less"; @import "twitter/bootstrap/forms.less"; @import "twitter/bootstrap/buttons.less";

// Components @import "twitter/bootstrap/component-animations.less"; @import "twitter/bootstrap/dropdowns.less"; @import "twitter/bootstrap/button-groups.less"; @import "twitter/bootstrap/input-groups.less"; @import "twitter/bootstrap/navs.less"; @import "twitter/bootstrap/navbar.less"; @import "twitter/bootstrap/breadcrumbs.less"; @import "twitter/bootstrap/pagination.less"; @import "twitter/bootstrap/pager.less"; @import "twitter/bootstrap/labels.less"; @import "twitter/bootstrap/badges.less"; @import "twitter/bootstrap/jumbotron.less"; @import "twitter/bootstrap/thumbnails.less"; @import "twitter/bootstrap/alerts.less"; @import "twitter/bootstrap/progress-bars.less"; @import "twitter/bootstrap/media.less"; @import "twitter/bootstrap/list-group.less"; @import "twitter/bootstrap/panels.less"; @import "twitter/bootstrap/responsive-embed.less"; @import "twitter/bootstrap/wells.less"; @import "twitter/bootstrap/close.less";

// Components w/ JavaScript @import "twitter/bootstrap/modals.less"; @import "twitter/bootstrap/tooltip.less"; @import "twitter/bootstrap/popovers.less"; @import "twitter/bootstrap/carousel.less";

// Utility classes @import "twitter/bootstrap/utilities.less"; @import "twitter/bootstrap/responsive-utilities.less";

If you'd like to alter Bootstrap's own variables, or define your LESS styles inheriting Bootstrap's mixins, you can do so inside bootstrapandoverrides.css.less:

@link-color: #ff0000;

SASS

If you are using SASS to compile your application.css (e.g. your manifest file is application.css.sass or application.css.scss) you may get this:

Invalid CSS after "*": expected "{", was "= require twitt..."
(in app/assets/stylesheets/application.css)
(sass)

If this is the case, you must use @import instead of

*=
in your manifest file, or don't compile your manifest with SASS.

Icons

By default, this gem (when using less generator) won't enable standard Bootstraps's Glyphicons.

If you would like to restore the default Glyphicons, inside the generated

bootstrap_and_overrides.css.less
uncomment these lines:
// Glyphicons are not required by default, uncomment the following lines to enable them.
@glyphiconsEotPath: font-url("glyphicons-halflings-regular.eot");
@glyphiconsEotPath_iefix: font-url("glyphicons-halflings-regular.eot?#iefix");
@glyphiconsWoffPath: font-url("glyphicons-halflings-regular.woff");
@glyphiconsTtfPath: font-url("glyphicons-halflings-regular.ttf");
@glyphiconsSvgPath: font-url("glyphicons-halflings-regular.svg#glyphicons_halflingsregular");

@import "twitter/bootstrap/glyphicons.less";

This gem was used to bundle the excellent Font-Awesome library. However that was another maintenance effort that is not worth to do here.

For Rails projects that need it there is the font-awesome-rails gem that provides same functionality that this gems was used to provide and it also have some other interesting features (e.g. view helpers).

Using JavaScript

Require Bootstrap JS (bootstrap.js) in your application.js

//= require twitter/bootstrap

$(function(){ /* Your JavaScript goes here... */ });

If you want to customize what is loaded, your application.js would look something like this

#= require jquery
#= require jquery_ujs
#= require twitter/bootstrap/transition
#= require twitter/bootstrap/alert
#= require twitter/bootstrap/modal
#= require twitter/bootstrap/button
#= require twitter/bootstrap/collapse

...and so on for each bootstrap js component.

Using CoffeeScript (optionally)

Using Bootstrap with the CoffeeScript is easy. twitter-bootstrap-rails generates a "bootstrap.js.coffee" file for you to /app/assets/javascripts/ folder.

jQuery ->
  $("a[rel~=popover], .has-popover").popover()
  $("a[rel~=tooltip], .has-tooltip").tooltip()

Using Helpers

Modal Helper

You can create modals easily using the following example. The header, body, and footer all accept contenttag or plain html. The href of the button to launch the modal must match the id of the modal dialog. It also accepts a block for the header, body, and footer. If you are getting a complaint about the modalhelper unable to merge a hash it is due to this.

Navbar Helper

It should let you write things like:















         {class: "navbar-form pull-right"} do |f| -%>
          <p></p>
          <p></p>
          <p></p>

Navbar scaffolding

In your view file (most likely application.html.erb) to get a basic navbar set up you need to do this:


Which will render:


Fixed navbar

If you want the navbar to stick to the top of the screen, pass in the option like this:


To render:


Static navbar

If you want a full-width navbar that scrolls away with the page, pass in the option like this:


To render:


Brand name

Add the name of your site on the left hand edge of the navbar. By default, it will link to rooturl. Passing a brandlink option will set the url to whatever you want.


Which will render:


Optional responsive variation

If you want the responsive version of the navbar to work (One that shrinks down on mobile devices etc.), you need to pass this option:


Which renders the html quite differently:


Nav links

This is the 'meat' of the code where you define your menu items.

You can group menu items in theoretical boxes which you can apply logic to - e.g. show different collections for logged in users/logged out users, or simply right align a group.

The active menu item will be inferred from the link for now.

The important methods here are menugroup and menuitem.

menu_group only takes one argument - :pull - this moves the group left or right when passed :left or :right.

menuitem generates a link wrapped in an li tag. It takes two arguments and an options hash. The first argument is the name (the text that will appear in the menu), and the path (which defaults to "#" if left blank). The rest of the options are passed straight through to the linkto helper, so that you can add classes, ids, methods or data tags etc.








          <p></p>
          <p></p>
          <p></p>

Dropdown menus

For multi-level list options, where it makes logical sense to group menu items, or simply to save space if you have a lot of pages, you can group menu items into drop down lists like this:









Dividers

Dividers are just vertical bars that visually separate logically disparate groups of menu items







Forms in navbar

At the moment - this is just a how to...

You need to add this class to the form itself (Different form builders do this in different ways - please check out the relevant docs)

.navbar-form

To pull the form left or right, add either of these classes:

css
.pull-left
.pull-right

If you want the Bootstrap search box (I think it just rounds the corners), use:

css
.navbar-search
Instead of:
css
.navbar-form

To change the size of the form fields, use .span2 (or however many span widths you want) to the input itself.

Component alignment

You can shift things to the left or the right across the nav bar. It's easiest to do this on grouped menu items:








Text in the navbar

If you want to put regular plain text in the navbar anywhere, you do it like this:

It also takes the :pull option to drag it to the left or right.

Flash helper

Add flash helper

 to your layout (built-in with layout generator).
You can pass the attributes you want to add to the main div returned: 


Breadcrumbs Helpers

Notice If your application is using breadcrumbs-on-rails you will have a namespace collision with the addbreadcrumb method. For this reason if breadcrumbs-on-rails is detected in

Gemfile
gem methods will be accessible using
boostrap
prefix, i.e. `render
bootstrapbreadcrumbs
and
add
bootstrap_breadcrumb`

Usually you do not need to use these breadcrumb gems since this gem provides the same functionality out of the box without the additional dependency.

However if there are some

breadcrumbs-on-rails
features you need to keep you can still use them and use this gem with the prefixes explained above.

Add breadcrumbs helper

 to your layout.
You can also specify a divider for it like this: 
') %>
(default divider is
/
). If you do not need dividers at all you can use
nil
:
.

Full example: ```ruby

renderbreadcrumbs(" / ", { class: '', itemclass: '', dividerclass: '', activeclass: 'active' })

```ruby
class ApplicationController
  add_breadcrumb :root # 'root_path' will be used as url
end
class ExamplesController < ApplicationController
  add_breadcrumb :index, :examples_path

def edit @example = Example.find params[:id] add_breadcrumb @example # @example.to_s as name, example_path(@example) as url add_breadcrumb :edit, edit_example_path(@example) end end

All symbolic names translated with I18n. See I18n Internationalization Support section.

Element utility helpers

Badge:

erb
 12

Label:

erb
 Gut!

Glyph: ```erb <%= glyph(:pencil) %>

<%= glyph(:pencil, {tag: :span}) %>

<%= glyph(:pencil, {class: 'foo'}) %> ```

I18n Internationalization Support

The installer creates an English translation file for you and copies it to config/locales/en.bootstrap.yml

NOTE: If you are using Devise in your project, you must have a devise locale file for handling flash messages, even if those messages are blank. See https://github.com/plataformatec/devise/wiki/I18n

Changelog

Please see CHANGELOG.md for more details

Contributors & Patches & Forks

Please see CONTRIBUTERS.md for contributors list

About Me

Senior Software Developer Istanbul / Turkey seyhunak [at] gmail com

Hire Me

Hire Me !

Thanks

Bootstrap and all twitter-bootstrap-rails contributors http://twbs.github.io/bootstrap

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]

License

Copyright (c) 2017 (since 2012) by Seyhun Akyürek

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.