Saturday, April 1, 2017

Bootstrap 4 -enabling Rails 5.02

To integrate bootstrap into my Rails 5.02 app, these are the steps taken by me :

1. rails new boots4rails -d mysql

2. rails g scaffold Todo title:string notes:text


      invoke    scss
      create      app/assets/stylesheets/todos.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss


3. rails db:create

C:\PRJ4RAILS\boots4rails>rails db:create
Created database 'boots4rails_development'
Created database 'boots4rails_test'

4. rails db:migrate

C:\PRJ4RAILS\boots4rails>rails db:migrate
== 20170326123016 CreateTodos: migrating ======================================
-- create_table(:todos)
   -> 0.1902s
== 20170326123016 CreateTodos: migrated (0.1914s) =============================

5. Add in db/seed.rb

Todo.create!(title: 'grocery shopping', notes: 'pickles, eggs, red onion')
Todo.create!(title: 'wash the car')
Todo.create!(title: 'register kids for school', notes: 'Register Kira for Ruby Junior High and Caleb for Rails High School')
Todo.create!(title: 'check engine light', notes: 'The check engine light is on in the Tacoma')
Todo.create!(title: 'dog groomers', notes: 'Take Pinky and Redford to the groomers on Wednesday the 23rd')

# db/seeds.rb

Then, 
C:\PRJ4RAILS\boots4rails>rails db:seed

6. Set up simple route in config/routes.rb

Rails.application.routes.draw do
  resources :todos
  root 'todos#index'

end


7. Start Rails Server - rails s


C:\PRJ4RAILS\boots4rails>rails s
=> Booting Puma
=> Rails 5.0.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 3.8.2 (ruby 2.2.6-p396), codename: Sassy Salamander
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

8.  goto localhost:3000


9.  Install ruby gem for bootstrap in gem file

gem 'bootstrap-sass', '~> 3.2.0'
gem 'autoprefixer-rails'

and bundle install

10.  Import Bootstrap CSS assets

Rename application.css to application.css.sass

C:\PRJ4RAILS\boots4rails\app\assets\stylesheets>ren application.css *.css.sass
Add in the renamed application.css.sass
@import "bootstrap-sprockets"
@import "bootstrap"



11.  Import Bootstrap Javascript Assets

Add in app/assets/javscripts/application.js like:

//= require jquery //= require jquery_ujs //= require turbolinks //= require bootstrap-sprockets //= require_tree .

Nb Critical that require_tree is last item. Also must come after require jquery
The reason is, //= require_tree . compiles each of the other Javascript files in the javascripts directory and any subdirectories. If you require bootstrap-sprockets after everything else, your other scripts may not have access to the Bootstrap functions.

12.  Now, goto localhost:3000


That's it :-) Compare the two screenshots before and after integrating bootstrap.

More at
CSS & Components

Javascript-based Features

Welcome to Rails.. Rails... Rails !

In 1995, I started the popular Clipper...Clipper... Clipper website (no blogs then) which was very popular and linked by virtually every Clipper-related site. When I switched to Windows via Delphi in 1997, I started the Delphi... Delphi... Delphi site. In June 2007, I discovered Ruby on Rails and no prize for guessing what I am gonna name this blog. which I started on 2nd October 2007.

As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam

CCH
10th June 2010, 19:42