Skip to content

Instantly share code, notes, and snippets.

## app/assets/javascripts/tenon/controllers/product_updates.js.coffee
Tenon.dispatcher.route 'tenon/product_updates#index', ->
new Tenon.features.AdvancedSearch
## app/controllers/tenon/product_updates_controller.rb
before_filter :initialize_advanced_search_params, only: :index
def index
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: postgresql #changed this
encoding: utf8
database: hosted_development #changed this
username: alan #changed this
password: MlwhvC4R
# Warning: The database defined as "test" will be erased and
# This creates Review model, controller, columns, and indexed movie_id column, and a one-to-many relationship between reviews movies.
rails g resource Review name:string stars:integer comment:text movie:references --no-test-framework
@HammerInTheNews
HammerInTheNews / form_for
Last active August 29, 2015 14:14
Add error message handling
# Put this in the form_for loop after "do"
<%= render 'shared/errors', object: @object_name %>
# Then in the shared/errors partial put:
<% if object.errors.any? %>
<section id="errors">
<header>
<h2>
Oops! The <%= object.titleize.downcase %> could not be saved.
</h2>
@HammerInTheNews
HammerInTheNews / Duplicates_Removed.rb
Created December 13, 2013 20:33
Remove Duplicates from arrays
dups = [8,1,2,1,4,4,5,7,8,5]
trimmed = dups.uniq
@HammerInTheNews
HammerInTheNews / CSV_generate.rb
Created December 13, 2013 20:32
Generate a CSV file – write rows to csvout.csv, read and print
require 'csv'
outfile = File.open('csvout.csv', 'wb')
CSV::Writer.generate(outfile) do |csv|
csv << ['c1', nil, 5]
csv << ['c2', 1, 2, 'abc']
end
outfile.close
#also read the file and print
@HammerInTheNews
HammerInTheNews / match_and_print.rb
Created December 13, 2013 20:29
Read all the lines of a text file into an array if they match a certain pattern.
lines = File.new('foo.txt', 'r').select do |line|
line =~ /pattern/
end
@HammerInTheNews
HammerInTheNews / strip_each_line.rb
Created December 13, 2013 20:29
Read all the lines of a text file into a list with some simple processing per line.
lines = File.new('foo.txt', 'r').collect() do |line|
line.strip()
end
@HammerInTheNews
HammerInTheNews / One Line at a Time.rb
Created December 13, 2013 20:27
Process each line in a text file one line at a time
File.new('foo.txt', 'r').each() do |line|
# Process the line
print line
end
@HammerInTheNews
HammerInTheNews / One Line at a Time.rb
Created December 13, 2013 20:27
Process each line in a text file one line at a time
File.new('foo.txt', 'r').each() do |line|
# Process the line
print line
end