Loading Heroku Mongo or Postgres Database on local environment

January 08, 2014

In case you would you like to run your local development environment to test with production data.

Step 1) Save a backup of the database to your computer from inside the PG Backups GUI or the Mongo GUI accessible from your Heroku Dashboard.

Step 2) Move it to whatever directory you are using in your console.

Step 3)


pg_restore --verbose --clean --no-acl --no-owner -h localhost

Mongo will dump as a bunch of .bson files in a folder instead of a single dump file.


mongorestore --host localhost --db DATABASE-NAME --drop 

Thinking outside the box: A way to help eliminate contact form spam

January 02, 2014

In an effort to help keep my mail box from being filled up with spam from my contact form, I tried out several different methods. The best method is using captcha but that is very invasive to the real user. I also tried a few other methods such has honeypot with varying levels of success.

The most effective way without being invasive to the real user is limit or eliminate the number of links. Spam bots always load you up with links.

Here is a Rails validation for saying no to hyperlinks. Hyperlinks most likely mean spam.

#in Model
validate :no_hyperlinks_in_body

def no_hyperlinks_in_body
    if body.include?("a href=")
      errors.add(:body, "Please do not send us any hyperlinks.")

Here is a Rails validation for allowing one link per message. In case a vendor or sponsor wanted to send you their website. In my opinion, any more than 1 link is spam.

# In Model
valiate :only_one_url_in_body

def only_one_url_in_body
    if body.scan("http://").count > 1
      errors.add(:body, "Please only one link per message.")

Adding indexes to MongodDB

August 15, 2013

# In model

class Blog
  include Mongoid::Document
  include Mongoid::Timestamps
  belongs_to :user, index: true

Then run the following rake command: rake db:mongoid:create_indexes

Exception Notification Emails

August 15, 2013

I went through the process of setting up email alerts if an exception error happens on my app. There is a nice railscast on how to use the exception_notification gem helped out a lot. I did have to make some tweaks though in order to get it work

# Gemfile

gem 'exception_notification'

# I added letter opener to test in development as recommended 
# in the railcasts. Here is how to get the gem to work as of today. 

group :development do
  gem "letter_opener", 
  git: "git://github.com/ryanb/letter_opener.git", 
  branch: "master"

# In production.rb or development.rb to test

ExceptionNotification::Rack, # (put this on the same line as above)
  email: {
    email_prefix: "[error] ",
    sender_address: %{"notifier" },
    exception_recipients: %w{test@example.com}

This information is all readily available, it just took some trial and error to see what combination actually worked. A nice simple and free way to be alerted whenever your app crashed on production.

Rails 4 routing with hyphens in the URL

August 06, 2013

# Routes (do this all on one line)
get '/page-with-hyphens',               
to: 'controller#page_with_hypens',                
as: 'page_with_hyphens'

This will give you the route of http://www.example.com/page-with-hyphens