Close

Update 7 - Ruby on Rails Install and Configuration

A project log for Smart Lock with Web Interface

Raspberry Pi based smart lock for apartment dwellers

chrisChris 12/29/2017 at 05:110 Comments

For the Ruby on Rails install there isn't really anything specific to a Raspberry Pi. I don’t need to copy something else's instructions here. Just follow steps 1-3 here to get started: http://parsun.com/2017/09/23/how-to-install-ruby-on-rails-on-raspberry-pi-3/

I forget why, but I also needed to install the ruby development package so something could compile during the install. If things freak out just try

sudo apt-get install ruby-dev 

then continue.

With that, you’re basically set except for some rails configuration. Download the repository by navigating to the folder you want the app stored in and running:

git clone https://github.com/cmoulder/smartlok

 
You will need to uncomment

# gem 'rpi_gpio'

in the gemfile if you are using a RPi. I’ve commented it out because it will only compile on a RPi, leading to one more problem for everyone else.

Then run through the normal rails setup. Make sure you’re in the same folder you installed the site:

bundle install
rails db:migrate
rails db:seed
rails s 

The development server should now be running on port 3000. Since we haven’t configured a firewall, we should be able to access this from another computer on the network with http://192.168.1.XX:3000, using whatever the local IP address is.

For actual usage, you will definitely want to configure your rails serer to run development mode. Personally, this is a little annoying to set up manually (as opposed to Heroku that does everything for you), but there aren’t that many steps

Start by making sure the server is shut down (Ctrl + C)

For the server to start, you’ll need to make a secret key for both rails and devise. Best practice is to save these as environment variables and not hard code them in your code. I do no such thing, but if you wanted you could do something like this in your config file:

config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production? 


Then set your environment variables, making sure you set them before we start rails during startup below.

I hard code them by generating two secret keys with

RAILS_ENV=production rake secret 

in the console. One goes in config/secrets.yml as secret_key_base = … under the production section for rails, and the other goes in config/initializers/devise.rb as config.secret_key = ….

More info on all of this is here: https://stackoverflow.com/questions/29187296/rails-production-how-to-set-secret-key-base

Next, we you need to migrate the database to the production environment (more info here: https://stackoverflow.com/questions/1949229/change-a-rails-application-to-production). In the console:

RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake db:seed

 
To start your site in production your need to tell to tell rails to do so. I run it at boot by having rc.local run a script located in my home directory

at the end of /etc/rc.local copy
sudo sh /home/pi/run_server.sh

sudo sh /home/pi/run_server.sh  


and in /home/pi/run_server.sh copy

echo 'Starting Rails'
cd /home/pi/sites/smartlok && sudo rails s -e production -p 1234 

 
The structure of the bottom line is 1. Navigate to my rails folder. 2. Start rails as a daemon in the production environment 3. Run it on port 1234 instead of a standard 80 (yours could default to 3000, but you should whatever port you want)

In rc.local above my rails line I also have

exec 2> /home/pi/rc.local.log # send stderr from rc.local to a log file
exec 1>&2 # send stdout to the same log file
set -x # tell sh to display commands before execution

 
which will save all of the console messages at boot to a log file (for the inevitable debugging when something doesn’t work).

I have already enable this for this site, but in general for all apps that you’re converting to production you either need to precompile your assets (javascript, css, etc) lest you upset the rails gods, or you can just let it compile them on the fly like in development. To compile on the fly set:

config.assets.compile = true 

in config/environments/production.rb

If you’re lucky everything will start on your next reboot. The default login is admin:password.


If you would like to configure email notifications, you will need to edit the server settings here: config/environments/production.rb

and edit the templates under: views/user_mailer

Discussions