Rails


Intercepting Action Mailer Emails in Rails


Create an Interceptor Module

To intercept emails in specific environments such as development, testing, or staging, we need to write some logic to do so.

module MailInterceptor
  def self.delivering_email(message)
    # This will be the email that live emails will be sent to 
    message.to = ['your-email@mail.com']

    # Set the cc and bcc to be nil to avoid sending out real emails
    message.cc = nil
    message.bcc = nil

    # Change the from if you want a di...

Read Blog Post


Compressing Documents To Zip


If you have a ton of uploaded files you need to have a visitor download, you will want to compress said information to decrease size and combine many files into one single zip. We will do this using the rubyzip gem. Keep in mind that if the files are audio or video, compressing them may not have any effect on the size.

Installation

We will first add the gem to our project.

# Gemfile
gem "rubyzip"

View

= link_to 'Download Zip', route_path(format: ...

Read Blog Post


Delayed Jobs


Sometimes when you have a project, you'll want to run really long scripts and have them daemonized and running in the background. This allows your server to scale and not hang a http request and cause long latency requests on your box.

First Step

Let's install a few gems that will help us do exactly this. I chose to use delay jobs gem over sidekiq, because I felt it would be better for my usage of it. Keep in mind that active job was added in Rails 4.2, so if you are running a versi...


Read Blog Post


Facebook Omniauth Rails


Setup a Facebook Developer Account


Integrate Omniauth


Add Omniauth To Your Gemfile

# Gemfile
gem 'omniauth-facebook'

Update The User Table With New Columns

rails g migration AddOmniauthToUsers provide...

Read Blog Post


Rails Migrations


Create a Migration

Say we have a column in our users table that we are no longer needing, we can generate a migration and add any needed information afterwards.

rails g migration RemovePhoneNumberFromUsers phone_number:string

Rename a Column

rails g migration RenamePhoneNumberFromUsers

Polymorphic Associations

rails g migration AddCommentableToComments body:text commentable:references{polymorphic}

Foreign Keys

...


Read Blog Post


Exporting CSV, PDF, XLS, XML, VCF in Rails


CSV Format

To save database information as a csv, we will need to require the rails csv library in the controller we are going to use it in. You also need to add the format in your view method you need it in.

# UsersController.rb
require 'csv'

def index
  # Your Code
  @users = User.all

  respond_to do |format|
    format.html
    format.csv
  end
end

Then in your index.csv.erb view file, you will need to add in the rows and columns you want to be displ...


Read Blog Post


Cool Rails Commands


Fun Things

If you ever want to connect to your own server or someone else's over your ethernet, use the following command in your terminal to start your server:

rails s -b 0.0.0.0 -p 3000

Wanting to run two different rails servers at once on the same machine? Simply change the ports the are running on to have them run simultaneously.

rails s -p 3000 
rails s -p 4000

If you need to add any images to your rails app with carrierwave or in ...


Read Blog Post