Sat Oct 07, 2017 4:00 PM

How to use PostgreSQL instead of SQLite with Rails on Rails

Assuming that you have Rails and PostgresSQL 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, run:


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 } %>

development:
  <<: *default
  database: exampleapp_development

test:
  <<: *default
  database: exampleapp_test

production:
  <<: *default
  database: exampleapp_production
  username: exampleapp
  password: <%= ENV['EXAMPLEAPP_DATABASE_PASSWORD'] %>


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

Thu Apr 26, 2018 1:51 PM
guide
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
Fri May 18, 2018 1:12 PM
guide
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
Wed Apr 18, 2018 8:23 PM
guide
Basic steps to create a new app using Rails API with a React frontend, then deploy on Heroku.
Mon May 21, 2018 2:58 PM
guide
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: http://itiansrock.blogspot.com/2013/01/truncate-and-link-text-helper-in-rails.htmlmore
Mon May 07, 2018 1:18 PM
guide
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