Skip to content

Rails 6 setup on Mac Catalina or BigSur, with Brew

Brian Xu edited this page Jun 30, 2022 · 31 revisions

These notes should work to build a new Rails6 project, using Active Scaffold, on a Mac. Tested by D. Bulgatz using Catalina, 10.15.7 with mysql 5.7, using Brew to install Ruby, mysql, and dependencies. Updated by Brian Xu using Big Sur 11.6.7.

Basic setup of Brew and Bundler

# skip this git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
brew update
brew install node
brew install yarn

brew install rbenv
rbenv install 2.7.4
brew install [email protected]

Create a folder for your application and install gems

mkdir rails6app
cd rails6app/

rbenv local 2.7.1
gem install bundler
gem install rails -v 6.0.3.5

Installing mysql2 Gem for mysql 5.7

gem install mysql2  -v '0.5.3' -- \
--with-mysql-config /usr/local/Cellar/[email protected]/5.7.29/bin/mysql_config \
--with-cppflags=-I/usr/local/opt/[email protected]/include \
--with-ldflags=-L/usr/local/opt/[email protected]/lib

Installing mysql2 Gem for mysql 8.0 with BigSur (new Cellar location)

gem install mysql2 -v '0.5.3' -- \
 --with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.27/lib \
 --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.27 \
 --with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.27/bin/mysql_config \
 --with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.27/include

Fix Default version of bundler

This may be necessary to not use an old/default version of bundler. It was for me.

gem update --system

Create the Rails app

setup for mysql, and with a specified version of Rails (Must be Pre 6.1)

#create the new Rails app
rails _6.0.3.5_ new myapp -d mysql

setup the application for Active Scaffold

#Add to Gemfile
gem 'active_scaffold', '3.6.10'
gem 'jquery-rails'
gem 'jquery-ui-rails' #needed for Date field searches to work

#For Export to excel, add these
gem "active_scaffold_export"
gem 'caxlsx'
gem 'caxlsx_rails'

#run Bundle install
bundle install

Setup your rails app for Active Scaffold

bundle exec rails g active_scaffold:install

Create app/assets/javascripts/application.js,

The new Rails 6 architecture has moved the javascript folder and renamed it!

//= require jquery
//= require jquery_ujs
//= require_tree .
//= require active_scaffold

Precompile the application.js

config/initializers/assets.rb

Rails.application.config.assets.precompile += %w( application.js )

Add this javascript include to main layout

app/views/layouts/application.html.erb

<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
Ensure this tag exists. 

Create a resource (use a generator to make life easy!)

rails g active_scaffold:resource vehicle m_id:integer chassis:string type:string yr_made:string yr_ended:string doors:string drive_train:string e_mnt:string hp:integer trq:integer e_id:integer trans:string

Run the migration created to build the new resource

If mysql is not currently running, type mysql.server start in terminal
bundle exec rails db:create
bundle exec rails db:migrate

Modify controller to setup for both search and field search and Export

app/controller/vehicles_controller.rb
conf.actions << :field_search
conf.field_search.link.parameters = {:kind => :field_search}
conf.field_search.link.label = 'Field Search'

#For excel export
conf.actions << :export
conf.export.default_file_format = 'xslx'

Add Stylesheets for Active Scaffold and Active Scaffold Export

app/assets/stylesheets/application.css

*= require active_scaffold
*= require active_scaffold_export

Start the Rails Server

bundle exec rails s

Navigate to newly created resource

http://localhost:3000/vehicles

Notes on Active Scaffold Export

be sure to define a to_s (to string) method in each model that will never return nil. There is a bug in the caxlxs gem that tries to determine the cell type, and it gets handed the active record object, which must respond to to_s or you will get an unhandled exception.

Clone this wiki locally