Adam Niedzielski

Programming is about people

How to remove Action Cable from a Rails app

| Comments

This is your complete guide to removing Action Cable from a Rails 5 app.

Some people need WebSockets in their app while other people do not need it at all or want to use other solution such as message_bus. Keep your application clean and delete Action Cable specific stuff.

Updated: 21.12.2017

Scenarios:

  1. I am upgrading from Rails 4
  2. I generated Rails 5 app with Action Cable
  3. I want to generate Rails 5 app without Action Cable

I am upgrading from Rails 4

1) Open config/application.rb.

2) If the file contains

1
require 'rails/all'

your are unnecessarily loading Action Cable code.

3) Replace require 'rails/all' with:

1
2
3
4
5
6
7
8
9
10
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"

4) (optional) Open Gemfile and replace:

1
gem "rails"

with:

1
2
3
4
5
6
7
8
9
gem "activerecord"
gem "activemodel"
gem "actionpack"
gem "actionview"
gem "actionmailer"
gem "activejob"
gem "activesupport"
gem "railties"
gem "sprockets-rails"

In general, list only the gems that you are really using as your dependencies. Please note that trimming down your Gemfile will only have an effect if none of your other dependencies depend on rails gem. You can verify that by searching for rails in Gemfile.lock.

I generated Rails 5 app with Action Cable

1) If in config/application.rb you have require 'rails/all' – replace it with:

1
2
3
4
5
6
7
8
9
10
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"

2) If in config/application.rb you have require "action_cable/engine" – remove this line.

3) Remove app/assets/javascripts/cable.js file.

4) Remove app/assets/javascripts/channels directory.

5) Remove app/channels directory.

6) Remove config/cable.yml file.

7) From config/environments/production.rb remove

1
2
3
4
# Mount Action Cable outside main process or domain
# config.action_cable.mount_path = nil
# config.action_cable.url = 'wss://example.com/cable'
# config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]

8) From Gemfile remove:

1
2
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'

9) If you added action_cable_meta_tag to app/views/layouts/application.html.erb, remove it from there.

10) (optional) Open Gemfile and replace:

1
gem "rails"

with:

1
2
3
4
5
6
7
8
9
gem "activerecord"
gem "activemodel"
gem "actionpack"
gem "actionview"
gem "actionmailer"
gem "activejob"
gem "activesupport"
gem "railties"
gem "sprockets-rails"

In general, list only the gems that you are really using as your dependencies. Please note that trimming down your Gemfile will only have an effect if none of your other dependencies depend on rails gem. You can verify that by searching for rails in Gemfile.lock.

I want to generate Rails 5 app without Action Cable

1) rails new my-app-name --skip-action-cable

2) (optional) Open Gemfile and replace:

1
gem "rails"

with:

1
2
3
4
5
6
7
8
9
gem "activerecord"
gem "activemodel"
gem "actionpack"
gem "actionview"
gem "actionmailer"
gem "activejob"
gem "activesupport"
gem "railties"
gem "sprockets-rails"

In general, list only the gems that you are really using as your dependencies. Please note that trimming down your Gemfile will only have an effect if none of your other dependencies depend on rails gem. You can verify that by searching for rails in Gemfile.lock.


If there is anything missing in this guide, please let me know! I want to keep it complete.

Comments