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

Thursday, March 30, 2017

What happens when you scaffold in Rails 5.02

 rails g scaffold Todo title:string notes:text

C:\PRJ4RAILS\boots4rails>rails g scaffold Todo title:string notes:text
      invoke  active_record
      create    db/migrate/20170326123016_create_todos.rb
      create    app/models/todo.rb
      invoke    test_unit
      create      test/models/todo_test.rb
      create      test/fixtures/todos.yml
      invoke  resource_route
       route    resources :todos
      invoke  scaffold_controller
      create    app/controllers/todos_controller.rb
      invoke    erb
      create      app/views/todos
      create      app/views/todos/index.html.erb
      create      app/views/todos/edit.html.erb
      create      app/views/todos/show.html.erb
      create      app/views/todos/new.html.erb
      create      app/views/todos/_form.html.erb
      invoke    test_unit
      create      test/controllers/todos_controller_test.rb
      invoke    helper
      create      app/helpers/todos_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/todos/index.json.jbuilder
      create      app/views/todos/show.json.jbuilder
      create      app/views/todos/_todo.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/todos.coffee
      invoke    scss
      create      app/assets/stylesheets/todos.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss

What happpens when you rails new in Rails 5.02

1. rails new boots4rails -d mysql


C:\PRJ4RAILS>rails new boots4rails -d mysql
      create
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/config/manifest.js
      create  app/assets/javascripts/application.js
      create  app/assets/javascripts/cable.js
      create  app/assets/stylesheets/application.css
      create  app/channels/application_cable/channel.rb
      create  app/channels/application_cable/connection.rb
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/jobs/application_job.rb
      create  app/mailers/application_mailer.rb
      create  app/models/application_record.rb
      create  app/views/layouts/application.html.erb
      create  app/views/layouts/mailer.html.erb
      create  app/views/layouts/mailer.text.erb
      create  app/assets/images/.keep
      create  app/assets/javascripts/channels
      create  app/assets/javascripts/channels/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  bin/setup
      create  bin/update
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/secrets.yml
      create  config/cable.yml
      create  config/puma.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/application_controller_renderer.rb
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/cors.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_framework_defaults.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/apple-touch-icon-precomposed.png
      create  public/apple-touch-icon.png
      create  public/favicon.ico
      create  public/robots.txt
      create  test/fixtures
      create  test/fixtures/.keep
      create  test/fixtures/files
      create  test/fixtures/files/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/test_helper.rb
      create  tmp
      create  tmp/.keep
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep
      remove  config/initializers/cors.rb
         run  bundle install
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Resolving dependencies...
Using rake 12.0.0
Using concurrent-ruby 1.0.5
Using i18n 0.8.1
Using minitest 5.10.1
Using thread_safe 0.3.6
Using builder 3.2.3
Using erubis 2.7.0
Using mini_portile2 2.1.0
Using rack 2.0.1
Using nio4r 2.0.0
Using websocket-extensions 0.1.2
Using mime-types-data 3.2016.0521
Using arel 7.1.4
Using bundler 1.13.6
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using method_source 0.8.2
Using thor 0.19.4
Using debug_inspector 0.0.2
Using multi_json 1.12.1
Using mysql2 0.4.5
Using puma 3.8.2
Using sass 3.4.23
Using tilt 2.0.7
Using turbolinks-source 5.0.0
Installing tzinfo 1.2.3
Using nokogiri 1.7.1
Using rack-test 0.6.3
Using sprockets 3.7.1
Using websocket-driver 0.6.5
Using mime-types 3.1
Using coffee-script 2.4.1
Installing uglifier 3.1.10
Using turbolinks 5.0.1
Using activesupport 5.0.2
Installing tzinfo-data 1.2017.2
Using loofah 2.0.3
Using mail 2.6.4
Using rails-dom-testing 2.0.2
Using globalid 0.3.7
Using activemodel 5.0.2
Using jbuilder 2.6.3
Using rails-html-sanitizer 1.0.3
Using activejob 5.0.2
Using activerecord 5.0.2
Using actionview 5.0.2
Using actionpack 5.0.2
Using actioncable 5.0.2
Using actionmailer 5.0.2
Using railties 5.0.2
Using sprockets-rails 3.2.0
Using coffee-rails 4.2.1
Using jquery-rails 4.3.1
Using web-console 3.4.0
Using rails 5.0.2
Using sass-rails 5.0.6
Bundle complete! 12 Gemfile dependencies, 56 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

Sunday, March 26, 2017

Let's Code - How to setup Bootstrap 4 on Ruby on Rails 5


Let's Code a quick and easy responsive website using Bootstrap 4 on Ruby on Rails 5! In this episode we setup Bootstrap 4 on a Rails application for some of that responsive and mobile first goodness.

For this Rails 5 app we use the following built-in Rails features:

- Generators (controller)
- Rubygems (Bootstrap 4, Tether)
- Asset Pipeline
- Layouts and views


Saturday, March 11, 2017

Using Notepad++ for writing Ruby Programs

Let us first configure Notepad++ for editing Ruby programs.
From the Notepad++ menu, click on Settings => Preferences. Click on Language Menu/Tab settings. Under the tab settings for [Default] modify the tab size and Replace by space options as shown below. We set the tab size to 2 and we set Replace by space option to use spaces instead of tab character.
Setting editor properties in Notepad++ for Ruby
Now we will add a custom menu in the Run menu for running Ruby programs. This way you can run Ruby programs directly from the Notepad++ editor. From the Notepad++ menu, click on Run => Run. This opens up the following window.
Configuring a shortcut for running Ruby programs in Notepad++
In the text field, enter the following command. In order for this to work, you need to have Ruby 1.9.x installed and the Ruby bin folder must be on the PATH environment variable. See this tutorial for step by step instructions on installing Ruby in Windows.
cmd /K ruby "$(FULL_CURRENT_PATH)"
Click on the Save… button to save this as a custom command.
Configuring a shortcut for running Ruby programs in Notepad++
Enter a name for the shortcut (Run Ruby Program) and then select a keyboard shortcut for running the command (F6). Click on OK.
Now from the Notepad++ menu, Click on Run. You will see the additional "Run Ruby Program" menu as shown below.
Custom Run Ruby option in Notepad++
In order for this shortcut to work, you need to ensure that your Ruby program is saved in a file. To test Notepad++, type in the following program,
1def fact(n)
2  if n==1
3    1
4  else
5    n * fact(n-1)
6  end
7end
8puts fact(5)
Save the file as factorial.rb. Now press F6 key or select the "Run Ruby Program" submenu from the Run menu. You should see the output of the Ruby program in a command window as shown below,

Monday, March 6, 2017

Rails 5 - has_secure_password

has_secure_password(options = {})
Adds methods to set and authenticate against a BCrypt password. This mechanism requires you to have a password_digest attribute.
The following validations are added automatically:
  • Password must be present on creation
  • Password length should be less than or equal to 72 characters
  • Confirmation of password (using a password_confirmation attribute)
If password confirmation validation is not needed, simply leave out the value for password_confirmation (i.e. don't provide a form field for it). When this attribute has a nilvalue, the  validation will not be triggered.
For further customizability, it is possible to suppress the default validations by passing validations: false as an argument.
Add bcrypt (~> 3.1.7) to Gemfile to use has_secure_password:
gem 'bcrypt', '~> 3.1.7'
Example using Active Record (which automatically includes ActiveModel::SecurePassword):
# Schema: User(name:string, password_digest:string)
class User < ActiveRecord::Base
  has_secure_password
end

user = User.new(name: 'david', password: '', password_confirmation: 'nomatch')
user.save                                                       # => false, password required
user.password = 'mUc3m00RsqyRe'
user.save                                                       # => false, confirmation doesn't match
user.password_confirmation = 'mUc3m00RsqyRe'
user.save                                                       # => true
user.authenticate('notright')                                   # => false
user.authenticate('mUc3m00RsqyRe')                              # => user
User.find_by(name: 'david').try(:authenticate, 'notright')      # => false
User.find_by(name: 'david').try(:authenticate, 'mUc3m00RsqyRe') # => user
More at http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html
https://rubyplus.com/episodes/311-Authentication-from-Scratch-in-Rails-5
https://rubyplus.com/articles/4171-Authentication-from-Scratch-in-Rails-5

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