← Home

Rails Generate channel

Reference & Command Builder

Published: 2023-05-11
Author: Harrison Broadbent
Type: Reference & Command Builder

Rails Generate Channel

Rails Generate Channel (rails g channel) is the generator for ActionCable channels. With ActionCable, you can integrate WebSockets into your Ruby on Rails apps for real-time features, like live chats or multiplayer collaboration.

A WebSockets/ActionCable channel is similar to a standard Rails controller; Consumers subscribe to one or more channels, with the channel coordinating the messages the consumer receives, and handling their responses.

By default, this generator creates both a channel.rb Ruby file, and a corresponding channel.js Javascript file.

Command Builder for rails g channel

rails g channel 

Channel name

Channel methods

Generator Options

Runtime Options

# Command Options
❯ rails g channel

  rails generate channel NAME [method method] [options]

      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
      [--assets], [--no-assets]                              # Indicates when to generate assets
                                                             # Default: true
  -t, [--test-framework=NAME]                                # Test framework to be invoked
                                                             # Default:

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

# Command Description
    Generates a new cable channel for the server (in Ruby) and client (in JavaScript).
    Pass the channel name, either CamelCased or under_scored, and an optional list of channel actions as arguments.

    bin/rails generate channel Chat speak

    creates a Chat channel class, test and JavaScript asset:
        Channel:    app/channels/chat_channel.rb
        Test:       test/channels/chat_channel_test.rb
        Assets:     $JAVASCRIPT_PATH/channels/chat_channel.js