Debugging Rails

Install pry, pry-byebug, and pry-rails gems.
To add a breakpoint, type binding.pry wherever you want your breakpoint to happen.
In console use coninue, step, next, finish commands:

step: Step execution into the next line or method. Takes an optional numeric argument to step multiple times.

next: Step over to the next line within the same frame. Also takes an optional numeric argument to step multiple lines.

finish: Execute until current stack frame returns.

continue: Continue program execution and end the Pry session.

To add shortcuts to ^ commands, add to ~/.pryrc:
Pry.commands.alias_command ‘c’, ‘continue’ rescue nil
Pry.commands.alias_command ‘s’, ‘step’ rescue nil
Pry.commands.alias_command ‘n’, ‘next’ rescue nil
Pry.commands.alias_command ‘f’, ‘finish’ rescue nil

Setting Up My Mac Dev Environment

Set up a package manager: Homebrew

Istall iterm2


Replace bash with zsh (it offers some nice plugins and themes).

brew install zsh zsh-completions

Install oh-my-zsh – “A delightful community-driven (with 1,000+ contributors) framework for managing your zsh configuration.”

sh -c "$(curl -fsSL"

To update oh-my-zsh, run upgrade_oh_my_zsh in zsh shell.

Pick your set of plugins (edit .zshrc file):
plugins=(git brew gem ruby rvm rails yarn npm)

Pick a theme (.zshrc):

If you're on OSX Sierra, then you need another step:
create ~/.ssh/config with this content:
Host *
  UseKeychain yes
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_rsa

Generate github SSH Keys –

Add generated ssh key to your keychain, so you don’t have to reenter your ssh passphrase:

 ssh-add -K ~/.ssh/id_rsa 


If using rvm with zsh, add this to your .zshrc:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

If you use rbenv, you may need to add this to your .zshrc:

export PATH="$HOME/.rbenv/shims :$PATH"
eval "$(rbenv init -)"


For exercism zsh completions, add

if [ -f ~/.config/exercism/exercism_completion.zsh ]; then
. ~/.config/exercism/exercism_completion.zsh

And run

$ mkdir -p ~/.config/exercism/
$ curl > ~/.config/exercism/exercism_completion.zsh

git standup


brew install git-standup

fzf is a general-purpose command-line fuzzy finder

brew install fzf
# Install shell extensions

autojump – a faster way to navigate your filesystem

brew install autojump

# add autoload to .zshrc
[[ -s $(brew --prefix)/etc/profile.d/ ]] && . $(brew --prefix)/etc/profile.d/

fkill – Fabulously kill processes

npm install --global fkill-cli
npm install --global alfred-fkill

Mac apps

Git alias (add to .gitconfig)

Show recent branches:
recent = “for-each-ref –sort=-committerdate –count=10 –format=’%(refname:short)’ refs/heads/”


Install Anaconda –

Update your .bashrc or .zshrc with

export PATH="$HOME/anaconda3/bin:$PATH"

Deploying Rails App to Heroku

My first experience installing a Rails app on Heroku. A pretty easy way to deploy Rails apps.

If you made any changes to your gemfile, make sure to run
bundle install –without production
Even if you already have all of the gems installed. This updates Gemfile.lock, which will be used by Heroku.

If you are on Windows, make sure to remove “.exe” from bundle, rails, and rake inside of your bin folder:
#!/usr/bin/env ruby.exe

Create a free account on Heroku if you don’t have one yet. Download the Heroku Toolbelt.
From the command line:

heroku login
cd path_to_you_app
heroku create

This will create a new Heroku application and set up the remote Git repo. You can optionally specify a name for your app in “heroku create” command, or you can rename it later.

Now, lets push the code to Heroku and run all the DB commands if necessary:

git push heroku master
heroku run rake db:migrate
heroku run rake db:seed
heroku open

To update you app, just push to heroku master.

To get information about your Heroku apps:

heroku apps
heroku ps
heroku logs
heroku run console

The last command will launch a remote Rails console session.

To rename your app:
heroku rename new_name

To destroy your app:
heroku destroy app_name

Starting with RSpec in Rails

Add RSpec related gems to gemfile:

group :development, :test do 
  gem 'rspec-rails'

group :test do
  gem "capybara"

Capybara is a framework for testing web applications; allows to navigate your app programmatically.

Install your RSpec gems: bundle install

Generate RSpec templates:
generate rspec: rails g rspec:install

To view all available generators: rails g

Since RSpec uses test database, run your migrations for test environment:
rails db:migrate RAILS_ENV=test
If this is the first run, may need to set up the database by running RAILS_ENV=test rake db:reset

To run all specs: rspec
To add formatting: rspec -format doc (or use shortcut: rspec -f doc)
To always use the same formatting, add -format doc to .rspec file.

To use helper files for RSpecs
Uncomment this line in rails_helper.rb:
Dir[Rails.root.join(‘spec/support/**/*.rb’)].each { |f| require f }
And place all your helper methods inside of spec/support directory.

Sample RSpec file:

require 'rails_helper'

describe 'Navigating movies' do
  it 'allows navigation from the detail page to the listing page' do
    movie = Movie.create(movie_attributes)

    visit movie_url(movie)

    click_link 'All Movies'

    expect(current_path).to eq(root_path)

Starting with Rails

Install Ruby. If you are on Windows, 2.3 seems to have issue as of September 2016, so I would go with 2.2.

Install Rails:
gem install rails –version 5.0.0 –no-ri –no-rdoc

Create a new application:
rails _5.0.0_ new application_name
use –skip-test or -T if you don’t want default test files or want to use a different test framework (Test::Unit is default)

Run the application:
rails s

If you are seeing something like this:

Could not load 'active_record/connection_adapters/sqlite3_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.

You probably have ruby version above 2.2 and you are on Windows. Downgrade to 2.2 or try a different OS.

Create a route for your first action:

verb "url" => "name_of_controller#name_of_action"
get 'movies' => 'movies#index'

Generate/destroy a controller:

rails g controller name_of_controller
rails destroy controller name_of_controller

If you don’t want to use the default framework, add –no-test-framework when generating a controller.

Update your view with something like this:

<% @movies.each do |movie| %>

<li> <strong><%= movie.title %></strong> (<%= movie.rating %>) <%= number_to_currency(movie.total_gross) %></li>
<% end %>

Create a model

rails generate model NAME [field[:type][:index] field[:type]
rails g model event name:string location:string price:decimal

To generate the whole resource:

rails g resource Resource_name column_name:column_type --no-test-framework

To destroy the resource:

rails destroy resource Resource_name column_name:column_type --no-test-framework

Use any of these DB column types types for your model:


To declare foreign key relationship: belongs_to :parent
To reference children in parent model: has_many :children
To cascade deletes: has_many :reviews, dependent: :destroy

To attach parent to child: child.parent = parent

Run migration:
rails db:migrate
rauks db:migrate:status

Use seeds.rb to create seed records for development
To run: rails db:seed

To look up commands:
rails -T
rails -T db

To Look up routes:
rails routes
In browser: {your_app_base_address}/rails/info/routes

for path/url use route_name_path/route_name_url
In rails command line (rails c):
<%= link_to("Whatever text", route_name_path) %>
Shortcut: <%= link_to("Whatever text", model) %>
Use _path in view templates and _url in controllers as redirects.

Routes with parameters
get ‘route/:id’ => ‘controller#show’, as: ‘some_route_name’
<%= link_to "Whatever text", route_name_path(model) %>
Shortcut: <%= link_to "Whatever text", model %>

Root route
root ‘controller#action’

Submitting Forms
Sample form:

<%= form_for(@movie, @review) do |f| %>
    <%= f.label :title %>
    <%= f.text_field :title %>
  <% Review::STARS.each do |star| %>
    <%= f.radio_button :stars, star %> <%= star %>
  <% end %>
    <%= f.submit %>
<% end %>

Updating a model

def update
  @movie = Movie.find(params[:id])

To allow specific model fields to be updated
params[:movie].permit(:title, :description)

Or (will fail if movie is not submitted)

To allow all fields to be updated:

Working with Active Record
Movie.order(‘released_on asc’)
Movie.order(released_on: :asc)

Movie.find_by(title: “Iron Man”)
Movie.where(rating: “PG-13”)
Movie.where.not(rating: “PG-13”)
Movie.where.not(rating: [“PG”, “PG-13”])
Movie.where(“total_gross < ?", 50000000) Movie.where("released_on > ?”,
Movie.where(“released_on <= ?","released_on desc") To view SQL use to_sql: Movie.where(rating: "PG-13").to_sql To rollback last migration: rails db:rollback
To see assets: http://base_url/assets/asset_name

Active Record methods on an instance:

Validation Examples:
validates :title, :released_on, :duration, presence: true
validates :description, length: { minimum: 25 }
validates :total_gross, numericality: { greater_than_or_equal_to: 0 }

validates :image_file_name, allow_blank: true, format: {
with: /\w+\.(gif|jpg|png)\z/i,
message: “must reference a GIF, JPG, or PNG image”
RATINGS = %w(G PG PG-13 R NC-17)
validates :rating, inclusion: { in: RATINGS }

To run something before each action:
before_action :method_name

One time messages.
To set:
flash[:notice] = “Message!”
If in redirect, can do:
redirect_to some_url, alert: “Alert!”
To read in view:
<%= flash[:notice] %>

Custom Flashes with redirects:
Rails only supports :notice and :alert by default for redirects. To add a custom one, add this to your ApplicationController: add_flash_types(:danger)

To set gems for specific environment:
group :development, :test do
gem ‘sqlite3’
To install gems excluding a group:
bundle install –without production

Nested resources
resources :parents do
resources :children

Setting up Ruby with Sublime

Install Ruby (through Chocolatey or or whatever else if you’re on Windows)

Install a couple Sublime plugins:

  • Linting: SublimeLinter + SublimeLinter-Ruby + SublimeLinter-rubocop (see docs for RuboCop on
  • Rubocop gem (if isung RuboCop) – “gem install rubocop”
  • GitGutter (if you use git)
  • AllAutocomplete
  • BeautifyRuby – requires HTML Beautifier gem (gem install htmlbeautifier)

Update some preferences (Preferences – Settings):

"auto_complete": true,
"auto_complete_commit_on_tab": true,
"copy_with_empty_selection": true,
"ensure_newline_at_eof_on_save": true,
"font_size": 11,
"index_files": true,
"tab_size": 2,
"translate_tabs_to_spaces": true,
"trim_trailing_white_space_on_save": true,
"word_separators": "./\\()\"'-:,.;<>[email protected]#$%^&*|+=[]{}`~"

Some great recommendations on


Starting Xamarin with MVVM binding in Code

Create your view model which implements INotifyPropertyChanged:

public class ViewModel : INotifyPropertyChanged
private string someValue;
public event PropertyChangedEventHandler PropertyChanged;

public string SomeValue
get { return someValue; }
if (someValue!= value)
someValue= value;

protected void OnPropertyChanged(string propertyName)
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

Bind it to your view:

var searchBar = new SearchBar();
searchBar.SetBinding(SearchBar.TextProperty, new Binding("SearchText", BindingMode.TwoWay));

If you need to retrieve your view model on some action:

var viewModel = BindingContext as ViewModel;
// update your viewModel then reassign to BindingContext
BindingContext = viewModel;