Skip to content

Instantly share code, notes, and snippets.

View tkbeili's full-sized avatar

Tammam Kbeili tkbeili

View GitHub Profile
@tkbeili
tkbeili / simple_form_with_rails.md
Last active March 3, 2016 17:28
SimpleForm with Rails Summary Notes

SimpleForm with Rails Summary Notes

Forms are a very common element of all web application so it's a good idea to have easy ways to generate forms. Rails comes with excellent views helpers: form_tag and form_for that help us generate forms. That said, there are even easier ways to generate forms using the SimpleForm gem. There is also another gem Formtastic that does something similar.

Setting Up

We start setting up SimpleForm by adding the gem to our Gemfile:

gem 'simple_form'

Then run bundle to install the gem. Then run

@tkbeili
tkbeili / deploy.md
Last active April 6, 2016 18:03
Deploying to Heroku

Deploying to Heroku

You need to use a gem called bundler, before deploying to Heroku. Create a new file in your app's main directory called Gemfile (with a capital 'G' and no extension). This Gemfile will allow us to require all the gems we need in one file, rather than manually requiring all the gems we need in our app.rb. Here we will use gem instead of require

# Gemfile
source 'http://rubygems.org'

gem 'sinatra'
gem 'pony'
gem 'data_mapper'
gem 'dm-postgres-adapter' # Note: Heroku only uses postgres
To have launchd start postgresql at login:
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
@tkbeili
tkbeili / fizzbuzz.rb
Last active August 29, 2015 14:01
FizzBuzz Solution
for number in 1..100
if number % 3 == 0 && number % 5 == 0
puts "FIZZBUZZ"
elsif number % 3 == 0
puts "FIZZ"
elsif number % 5 == 0
puts "BUZZ"
else
puts number
end
@tkbeili
tkbeili / addresses
Last active August 29, 2015 14:00
Random Addresses For Testing Purposes
2335 Canton Hwy #6 Windsor ON N8N 3N2
6 Arch St #9757 Alcida NB E8J 2C4
9547 Belmont Rd #21 Belleville ON K8P 1B3
73 Pittsford Victor Rd Vancouver BC V5Z 3K2
447 Commercial St Se LIle-Perrot QC J7V 4T4
47 Garfield Ave Swift Current SK S9H 4V2
3 Mill Rd Baker Brook NB E7A 1T3
136 W Grand Ave #3 Delhi ON N4B 1C4
80 Maplewood Dr #34 Bradford ON L3Z 2S4
58 Hancock St Aurora ON L4G 2J7
@tkbeili
tkbeili / .irbrc
Last active August 29, 2015 14:00
# ruby 1.8.7 compatible
require 'rubygems'
require 'irb/completion'
# interactive editor: use vim from within irb
begin
require 'interactive_editor'
rescue LoadError => err
warn "Couldn't load interactive_editor: #{err}"
end
class TriangleClimber
def initialize(size = nil, grid = nil)
@possible_ways = 0
@grid = grid ? grid.inject([]) {|r,e| r << e.strip.split(" ")}.reverse : nil
@size = size || @grid.size
end
def find_possible_climbing_ways
0.upto(@size - 1) {|x| move([0, x]) unless grid_has_trap?([0, x])}
@tkbeili
tkbeili / gist:8423267
Last active March 16, 2016 20:38
My .irbrc
# ruby 1.8.7 compatible
require 'rubygems'
require 'irb/completion'
# interactive editor: use vim from within irb
begin
require 'interactive_editor'
rescue LoadError => err
warn "Couldn't load interactive_editor: #{err}"
end
@tkbeili
tkbeili / One to Many Relationship
Created March 25, 2013 16:11
Steps for creating one to many relationship with SQL database in Rails
1- Decide which one is the “parent” and which one is the “child”. So the parent has_many children
2- If you are using SQL database add a column to the child ActiveRecord model named parent_id
3- In the parent ActiveRecord model add has_many :children (notice we used plural in here)
4- In the child ActiveRecord model add belongs_to :parent (notice we used singular in here)
5- (optional) Change the child controller to be nested from the parent’s controller in order to re-use methods (mostly for before_filter) by:
a- Makes the routes for the child controller nested inside the “show” action of parent controller
b- Make the child controller inherit from the parent controller.
c- Setup filters as needed
d- Change URLs relating to child controller around your app.
require 'spec_helper'
describe "Questions" do
describe "GET /questions" do
it "Should respond with success to /quetsions" do
get questions_path
response.status.should be(200)
end
end