The project is in a healthy, maintained state
Flash message injection for Ruby on Rails Turbo Stream responses
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 6.1
 Project Readme

Rails Turbo Flash

Automatic flash message injection for Ruby on Rails Turbo Stream responses.

Installation

Add rails_turbo_flash to your Gemfile, or:

$ bundle add rails_turbo_flash

Getting Started

  1. Include the controller helper:

    class ApplicationController < ActionController::Base
      include RailsTurboFlash::Callbacks
    end
  2. Use the view helper to render a turbo stream target into your top-level layouts. For example, in app/views/layouts/application.html.erb:

    <%= turbo_flash_tag %>
    
    <%# Or with some additional attributes: %>
    
    <%= turbo_flash_tag class: 'fixed flex gap-8 justify-center top-0 right-0 z-50' %>
  3. Add flash messages in your controller actions with flash.now.

That's it!

Customizing

To customize Rails Turbo Flash, create a file config/initializers/rails_turbo_flash.rb.

The default values are shown below:

RailsTurboFlash.configure do |config|
  config.action = :prepend # The turbo stream action
end

To make Rails Turbo Flash look like your app, override the bundled views by adding them to your app. You can manually copy the specific views that you need to app/views/turbo_flash, or copy them to your application with the included generator:

$ rails generate rails_turbo_flash:views

See the bundled views.

Why and How

Unable to find an authoritative solution from the Turbo Rails team or Rails community for adding flash messages to turbo stream responses, I hacked this gem together. It aims to be DRY, simple, and unobtrusive. This solution was tailored specifically to my needs and works for me; I make no guarantees it's good for everyone.

Basically, it checks for a turbo_stream request format and appends a turbo stream to the response body via an after_action callback. The entire implementation is only a few lines of code.

Contributing

Here are a few ways you can help:

License

Open source under the terms of the MIT License.