How to use PostgreSQL instead of SQLite with Ruby on Rails

Assuming that you have Rails and PostgreSQL already installed on your workstation, follow the steps for a new or existing Ruby on Rails app according to your needs.

Use PostgresSQL with a new Rails app

Run the following command to create a new Ruby on Rails app using PostgresSQL adapter:

rails new myapp --database=postgresql

Now, switch into the newly created app directory and run in the terminal:

rake db:create

Add PostgreSql to an existing Ruby on Rails app

Add the following line to your gemfile, then run bundle install

gem 'pg'

Update the file config/database.yml to match the below code. Replace "exampleapp" instances with the name of your app.

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

  <<: *default
  database: exampleapp_development

  <<: *default
  database: exampleapp_test

  <<: *default
  database: exampleapp_production
  username: exampleapp

You will need to then recreate your database by running the following:

rake db:drop db:create db:migrate

As a shortcut, you can run the migrate reset command instead

rake db:migrate:reset

If you have a seed file, use this instead:

rake db:drop db:setup

As a shortcut, you can run the reset command instead

rake db:reset

The ability to like or favorite posts is very common on websites. This guide will show you how to add this functionality to your Rails 5 application without adding unnecessary bloat from gems. This guide assumes that you have already implemented posts (or similar) and users to your application and will not cover these steps. Let's go over some design considerations before we begin. While everyone can view a post, only authenticated users can like posts by clicking a link or icon.  We're keeping it positive by just tracking likes.more
For use with trusted/cleaned data. Add to application.rb def truncate_and_link(text,options ={}) length = options[:length] return text if length.blank? url = options[:url] || '#' output = raw text.truncate_words(length) output += link_to('more', url) if text.size > length output.html_safe end Adapted from:
Basic steps to create a new app using Rails API with a React frontend, then deploy on Heroku.
Recently, while creating a subscription checkout form using Stripe Elements, I wanted to list the plans along wth their pricing and details. The form uses a collection input to list the plans as radio buttons, but the methods in this guide should work for checkboxes and selects.  Here is the input statement that we are working with for starters: <%= f.input :plan_id,   collection: Plan.published, as: :radio_buttons %> Currently, this will display the following label and radio buttons (Note that this label is provided using I18n): Please select your plan: ○ plan 1 ○ plan 2 I already have text with instructions...more
Problem: Ransack will only search for one keyword or phrase such as, "Jumpity Jump", without quotations. Using quotations or commas such as in "Jumpity Jump" or, "Jumpity, Jump" (with or without quotations). returns a blank result. Test conditions: Randomly selected records were edited to include varied words/phrases in searched fields, such as in the following examples: jump jumpity jump jimpity jump jump rabbit jumpity jumpity rabbit Solution: Custom predicates added to initializer based on Stack Overflow answer linked at end.more
Font Awesome 5 SVG icons use javascript to find <i> tags with an icon class and replaces them with an <svg> tag. Turbolinks online displays these icons on initial page load and/or refresh, however when following a link, Turbolinks reloads the page and not the icons. This guide assumes that you have already loaded font awesome into your project using your preferred method. For what its worth, this guide uses the font awesome CDN more