ruby-on-rails

Turbo Will Call Stimulus `connect()` Twice

When you click a link to return to a page, or use your browsers back button to return to a page Turbo will render a cached preview of the page. It will then fetch an updated version of the page. If you have a stimulus controller on the page what will happen is the controller is initialized twice. The initial cached version will load the controller and fire off the connect() method, then the page is refreshed via Turbo, the controller is disconnected, then reconnected when the DOM is updated from the Turbo update.

Continue reading →

Running Ruby on Rails on Docker

I have been developing Ruby on Rails apps in Docker for several years now. I couldn't imagine not using Docker at this point! An Introduction to DockerDocker an open-source project for automating the deployment of applications as portable self sufficient containers that run in cloud or on premises. Docker is also a company that owns this technology. The underlying technology that powers Docker has been part of Linux for many years.

Continue reading →

Getting Familiar with RSpec in Rails

I spent some time over the weekend getting familiar with RSpec. Gonna brain dump (with just a little bit of structure) the process and what I did and learned. To start I set up in a new rails project and kinda tweaked it into a place where I can be productive. What is RSpec though? It is a testing framwork. But it's a little different than the Minitest testing framework that ships with Rails.

Continue reading →

The difference between length, size, and count in Ruby on Rails

I was asked recently if I could explain the difference between length, size, and count for ActiveRecord models in Ruby on Rails. Unfortunately I had no answer. But I wanted to really understand so I dug into the API docs. On a Ruby Array the methods size, count, and length are as such; size is an alias for length, length simply returns a count of the all elements in the array, and count is pretty neat in that you can pass in a block to count elements based on some condition.

Continue reading →

Deploying a Simple Rails App with Ansible

Ruby on Rails is quickly becoming my framework of choice for my personal websites and projects. It's a pleasure to work with and has been easy to learn. But no framework is without its challenges. One of those challenges is of course deploying the app to a server. There are a lot of options for hosting and deploying a Rails app. But, I like to run my own servers which means I have to also take care of deploying to those servers.

Continue reading →

How to Run Rails App Server with Systemd and Ansible

Create a systemd service to run your rails app server. Ansible tasks to create the service: --- …snip… vars: rails_root: “/myapp” rails_user: “webuser” tasks: - name: Setup Rails Web Service template: dest: /usr/lib/systemd/system/rails-web.service src: templates/rails-web.systemd.j2 - name: Enable Rails Web Service systemd: name: rails-web daemon_reload: yes enabled: yes masked: no The ansible template "rails-web.systemd.j2": [Unit] Description=Rails Web [Service] Type=simple SyslogIdentifier=rails-web User={{ rails_user }} PIDFile={{ rails_root }}/tmp/pids/web.pid WorkingDirectory={{ rails_root }} ExecStart=/bin/bash -l -c “{{ rails_root }}/bin/rails s -b 0.

Continue reading →