Categories
Ruby on Rails

How to add Bootstrap 4 to your Ruby on Rails Application

Luckily adding Twitter Bootstrap 4 to your ruby on rails application is not too complex.

Here’s the quick and dirty steps to adding bootstrap to your rails application.

Launch your app

Move into your existing application directory or run rails new myapp to create a new application.

Add the bootstrap and jquery gems to your Gemfile.

gem 'bootstrap', '~> 4.4.1'
gem 'jquery-rails'

Run bundle install --verbose (it may take a while).

Restart your server if you have it running. This is an important step for ensuring bootstrap is available in the applications pipeline.

Import styles in application.scss

Rename the default app/assets/stylesheets/application.css to a .scss file.

mv app/assets/stylesheets/application.css app/assets/stylesheets/application.scss

Remove all the *= and *= require_tree statements to have access to Bootstrap mixins and variables.

Importing bootstrap at app/assests/stylesheets/application.scss

Add jQuery

Bootstrap 4 relies on jQuery for all the JavaScript heavy lifting.

Earlier we added the ‘jquery-rails gem’ and ran bundle install.

Now all we have to do is add bootstrap dependencies to our app/assests/javascripts/application.js

//= require jquery3
//= require popper
//= require bootstrap
Add bootstrap dependencies to your application.js file.

Check the source code

To test that bootstrap was added to our applications assets pipeline, launch the server and check the source code.

Checking the applications source code for links to bootstrap.

You did it!

What did we do?

  • Added the Bootstrap and Jquery gems to our Gemfile and ran bundle install --verbose.
  • Renamed our application.css to a .scss file and added @import "bootstrap";
  • Added jQuery dependancies to application.js
  • Confirmed what we did by launching the app and checking the source code.
Categories
Ruby on Rails

How to setup Rails App for Heroku

If you want to host your Ruby on Rails application with Heroku- your database must be PostgreSQL.

Unfortantely, it can be difficult to get setup if you’ve never done anything like use the Heroku CLI or manually create databases in psql.

Here are the quick and dirty steps for setting up your rails application for hostign on heroku.

Install Heroku

If you haven’t installed heroku-cli, that is step number one.

Visit the download instructions to figure out how to install heroku.

For me (xubuntu) I used snap.

sudo snap install --classic heroku

Next, you’ll be prompted to setup/login with Heroku.

Create the application

If you are starting from scratch, run the rails new command and specify you want a postgreSQL database (since thats what heroku requires).

rails new myapp –database=postgresql

Move into your new application and run bundle install.

cd myapp
bundle install
...
Bundle complete! 18 Gemfile dependencies, 78 gems now installed.

Test the databases

For my setup, I have to manually create the databases in PostgreSQL.

Run the following commands, using your application’s name instead of myapp.

psql
CREATE DATABASE myapp_development;
CREATE DATABASE myapp_test;
\list;

If psql didn’t work or needs to be setup, I wrote a short post about getting setup with PostgreSQL.

Go back into your application and run rails db:migrate to test your databases were added correctly.

No error message? Good!

Create a welcome page

Rails 5.x no longer includes a default page in a production environment.

So we will have to make one! First create a controller.

rails g controller pages home

Now create a home.html.erb file in the app/views/pages/ directory.

Add some simple HTML to the homepage.

<h1>Rails!</h1>
<a href="http://gerrg.com">GERRG.com Learn to build a Rails Application and Host with Heroku</a>

Finally add the route to the newly create homepage. Open up config/routes.rb and add the following line.

root 'pages#home'

Open up your terminal and launch your server with rails server.

Add it to Github

Now that you’ve got your self a fancy new rails application complete with default page in production environments…

Its time to add it to GitHub before we host the application on Heroku.

Setup repository on GitHub: add, commit and push application.

git add -A
git commit -n 'init commit'
git push

Your push will not work if you haven’t setup your remote origin. In fact, you’ll get an error message about it.

Host on Heroku

Okay so far we have:

  • created our rails application
  • setup our postgreSQL databases
  • built a default page for our production environment
  • and created a repository on github.

Finally, we are going to host our rails application with Heroku and have our site out there on the world wide web.

Run Heroku create to create a repository with Heroku, then push your application from git to Heroku.

heroku create
git push heroku master
This push will take a while and you will see lots of scary messages… just let it finish.

If the command completes, the push was complete and you should be able to visit your application with heroku open.

you did it!

Categories
Ruby on Rails

How to fix “A server is already running.” error in Ruby on Rails

Luckily this is an easy fix. You error message will more accurately say something along the lines of:

A server is already running. Check /home/greg/odin/spitter/tmp/pids/server.pid.

If you run cat on the file referenced in the message, it will return a 5 digit number like 30351.

cat /home/greg/odin/spitter/tmp/pids/server.pid returns the ID of the process.

30351 is the ID of the process running the current rails server. Run kill -9 30351 to end the process and free up room for a new server.

You are now free to run rails s to boot up the server.

Categories
Ruby on Rails

How to create PostgreSQL database for Ruby On Rails

This is a note to myself and those who are interested.

GERRG

I use Heroku for hosting my Ruby on Rails applications. Problem is, Heroku doesn’t support SQlite databases.

If you’ve ever tried to run rails db:migrate to create a postgesSQL database WITHOUT first setting up the databases manually, you will recognize this error:

rails aborted!
ActiveRecord::NoDatabaseError: FATAL:  database "spitter_development" does not exist

Setup a PostgreSQL database

Run the following command.

psql

If you get this error, your package manager did not setup your user account up properly.

createdb: database creation failed: ERROR:  source database "template1" is being accessed by other users

Run the createdb command.

$ createdb
$ psql
psql (10.10 (Ubuntu 10.10-0ubuntu0.18.04.1))
Type "help" for help.

Create the databases

Once you are in psql, run \list to see all the databases on your server (service)

before we add the databases

Now run the CREATE DATABASE command followed by the name of your rails applications and finally suffixed with _development and _test respectively.

Dont forget the semi-colons! ( ; )

CREATE DATABASE yourapp_development;
CREATE DATABASE yourapp_test;

Run the \list command again to confirm your databases were added correctly.

with new databases added